yilmaz
28.11.22, 21:20
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.
<?php
if (extension_loaded("curl")) {
echo "Curl is installed.";
} else {
echo "Curl is not installed.";
}
?>
Demo: https://www.jdoodle.com/a/5yFC
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.
<?php
$ch = curl_init('https://www.yilmaztv.com/demo/test.html');
curl_exec($ch);
curl_close($ch);
?>
Demo: https://www.jdoodle.com/a/5yFH
The above example is made with the curl_setopt function.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
curl_exec($ch);
curl_close($ch);
?>
We said that the most important process is to make the appropriate settings with curl_setopt .
In the example below, the value retrieved is assigned to a variable.
<?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);
?>
Demo: https://www.jdoodle.com/a/5yFJ
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.
<?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);
?>
Similarly, it can be done with various CURL settings and PHP file processing function.
<?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 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.
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.
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.
<?php
if (extension_loaded("curl")) {
echo "Curl is installed.";
} else {
echo "Curl is not installed.";
}
?>
Demo: https://www.jdoodle.com/a/5yFC
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.
<?php
$ch = curl_init('https://www.yilmaztv.com/demo/test.html');
curl_exec($ch);
curl_close($ch);
?>
Demo: https://www.jdoodle.com/a/5yFH
The above example is made with the curl_setopt function.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.yilmaztv.com/demo/test.html');
curl_exec($ch);
curl_close($ch);
?>
We said that the most important process is to make the appropriate settings with curl_setopt .
In the example below, the value retrieved is assigned to a variable.
<?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);
?>
Demo: https://www.jdoodle.com/a/5yFJ
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.
<?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);
?>
Similarly, it can be done with various CURL settings and PHP file processing function.
<?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 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.
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.