1. Basic steps of cURL request:
(1) Initialization
(2) Set options, including URL
(3) Execute and get HTML document content
(4) Release the cURL handle
The code copy is as follows:
<?php
//1. Initialization
$ch = curl_init();
//2. Set options, including URL
curl_setopt($ch, CURLOPT_URL, "http://www.cnblogs.com/it-cen/");
//Return the information obtained by curl_exec() as a file stream instead of directly outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//When starting, the header file information will be output as a data stream
curl_setopt($ch, CURLOPT_HEADER, 1);
//3. Execute and get HTML document content
curl_exec($ch);
//4. Release the handle
curl_close($ch);
echo $ch;
?>
Note: The second step is the most important, that is, the curl_setopt() function
We can add a statement to check for errors. Here we should pay attention to using "===false", which is to distinguish between empty output and boolean value false
The code copy is as follows:
$output = curl_exec($ch);
if ($output === false) {
echo "cURL Error:".curl_error($ch);
}
The curl_getinfo() function returns information about this request after cURL is executed, which is useful for debugging and troubleshooting:
The code copy is as follows:
curl_exec($ch);
$info = curl_getinfo($ch);
echo '<pre>';
print_r($info);
echo '</pre>';
Returned data
The code copy is as follows:
Array
(
[url] => http://www.cnblogs.com/it-cen/
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 312
[request_size] => 61
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.172
[namelookup_time] => 0.016
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 14658 <span style="color: #ff0000;"> //Requested data size</span>
[speed_download] => 85220
[speed_upload] => 0
[download_content_length] => 14658
[upload_content_length] => 0
[starttransfer_time] => 0.125
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] =>
)
2. This information is very useful in debugging . For example, when cURL crawling, the crawling data may often occur due to network reasons, and we can calculate the filesize through the acquired data, and then compare it with the obtained data. If the sizes are equal, it is determined that the download is correct, otherwise we will make repeated attempts.
Let's take a look at an example of crawling images:
The code copy is as follows:
<?php
header("Content-Type: image/png");
//1. Initialization
$ch = curl_init();
//2. Set options, including URL
curl_setopt($ch, , CURLOPT_URL, "http://img04.taobaocdn.com/tfscom/TB1omaTHXXXXXajXVXXtKXbFXXX.png");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//3. Execute and get content
$res = curl_exec($ch);
//Get information
$info = curl_getinfo($ch);
//4. Release resources
curl_close($ch);
file_put_contents("d:/aa.png", $res);
$size = filesize("d:/aa.png");
if ($size != $info['size_download']) {
echo "The downloaded data is incomplete, please download it again";
} else {
echo "Download data complete";
}
?>
3. Send data using POST method in cURL
The code copy is as follows:
<?php
$ch = curl_init();
$data = array('name'=>'kelly', 'age'=>27, 'sex'=>1);
curl_setopt($ch, CURLOPT_URL, "http://localhost.post.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set as post
curl_setopt($ch, CURLOPT_POST, 1);
//Add the post variable
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$res = curl_exec($ch);
curl_close($ch);
echo $res;
?>
This method can simulate messages, or you can sit on a watering robot, and the ideas are the same
4. Upload files with cURL
The code copy is as follows:
<?php
//Request for uploaded data
$data = array('name'=>'beauty', "upload"=>"@a.zip");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/Socket/upload_file.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
curl_close($ch);
echo $res;
?>
Note: When sending a file, prefix the @ before the file name and use the full path
5. cURL settings item
In fact, cURL has many configuration options, and these options are the soul of cURL. Setting it through setopt(), the following summarizes several common and important configuration items. I hope it will be of some help to readers when using cURL in the future:
CURLOPT_AUTOREFERER: When redirecting according to location:, the Referer: information in the header is automatically set.
CURLOPT_COOKIESESSION: When enabled, cURL will pass a sessioncookie tightly, ignoring other cookies
CURLOPT_HEADER: Output the header file information as a data stream
CURLOPT_INFILESIZE: Set the size of the uploaded file, in bytes
CURLOPT_MAXCONNECTS: Maximum number of connections allowed
CURLOPT_MAXREDIRS: Specify the maximum number of HTTP redirects
CURLOPT_COOKIE: Set the content of the "cookie:" part of the HTTP request. Multiple cookies are followed by a semicolon, and a space is added to the semicolon.
CURLOPT_POSTFIELDS: All data is sent using the "POST" operation in the HTTP protocol to send the file to be sent, and the file name is prefixed by the @ prefix and the full path is used.
.........
For more configuration items, please refer to the PHP manual
cURL is very powerful, it is a general library, not unique to PHP.
I hope readers can gain something through learning several classic cURL examples in this blog post.