It contains information about the installation, use and downloading of the CURL library, which is used for obtaining information from web pages, making bots, data extraction.
What is CURL?
CURL (Client URL) is a software project that allows data exchange between various protocols.
CURL is divided into two as Libcurl and CURL.
Libcurl C, C++, Java, C# etc. It provides data exchange from various protocols by acting as an interface for programming languages.
CURL, on the other hand, uses the Libcurl library to exchange data from the command interpreter (CMD, PowerShell, Terminal, etc.) of the operating system.
PHP CURL installation
You can use the commands below to check the CURL installation.
Demo: https://www.jdoodle.com/a/5yFCPHP Code:
<?php
- if (extension_loaded("curl")) {
- echo "Curl is installed.";
- } else {
- echo "Curl is not installed.";
- }
- ?>
After enabling the CURL library, you can complete the installation by restarting the server software for the settings to take effect.
If there is nothing of the CURL library in the PHP settings file, you can obtain the necessary files for CURL installation at https://curl.se/ .
Using PHP CURL
Within the PHP CURL structure, functions are defined for various operations.
With PHP, curl_init , curl_setopt , curl_exec , curl_close functions are used in CURL operations.
curl_init
The function is used to start the CURL operation. Depending on the use of the function, it defines the address it receives with the parameter as the address to be connected.
curl_setopt
It is an important function that determines the settings to be used during CURL operations. The function provides different operations such as downloading files, extracting data, sending data.
curl_exec
Runs the CURL session with the settings and acts according to the CURL settings.
curl_close
Terminates the CURL session.
The following example shows the site link with CURL.
Demo: https://www.jdoodle.com/a/5yFHPHP Code:
<?php
- $ch = curl_init('https://www.yilmaztv.com/demo/test.html');
- curl_exec($ch);
- curl_close($ch);
- ?>
The above example is made with the curl_setopt function.
We said that the most important process is to make the appropriate settings with curl_setopt .PHP Code:
<?php
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
- curl_exec($ch);
- curl_close($ch);
- ?>
In the example below, the value retrieved is assigned to a variable.
Demo: https://www.jdoodle.com/a/5yFJPHP Code:
<?php
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- echo htmlspecialchars($result);
- ?>
In the example , the curl_setopt function and the CURLOPT_RETURNTRANSFER setting are enabled so that the curl_exec function returns the received value.
With the return value, it is possible to print to a file or to get the desired values with the HTML parser (HTML Parser).
CURL settings may vary depending on the operation and protocol.
PHP CURL file download
We said that the appropriate CURL setting should be determined according to the process in the PHP CURL usage title.
Downloading files with CURL can be done simply as follows.
Similarly, it can be done with various CURL settings and PHP file processing function.PHP Code:
<?php
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- file_put_contents('test.html', $result);
- ?>
When downloading files with CURL, it should be noted that the process will take a long time depending on the file size and the PHP command execution time limit specified in the PHP settings will be exceeded.PHP Code:
<?php
- $ch = curl_init();
- $dosya = fopen('test.html', 'w');
- curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FILE, $dosya);
- $result = curl_exec($ch);
- curl_close($ch);
- fclose($result);
- ?>
When downloading files with CURL, it will be useful to change the command execution time in the PHP settings.
I wish you a good day.