This article mainly introduces several classic application examples of CURL in PHP. This article describes the basic steps of cURL requests, uses the POST method to send data in cURL, and uses cURL to upload files, for more information, see
1. basic steps for cURL requests:
(1) Initialization
(2) set options, including URL
(3) execute and obtain HTML document content
(4) release the cURL handle
The code 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 () in the form of a file stream, instead of directly outputting
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// The header file is output as a data stream at startup.
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
// 3. execute and obtain the 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 check error statement. here we should note that "= false" is used to distinguish between null output and boolean value false.
The code is as follows:
$ Output = curl_exec ($ ch );
If ($ output = false ){
Echo "cURL Error:". curl_error ($ ch );
}
The curl_getinfo () function returns information about the request after the cURL is executed, which is useful for debugging and Troubleshooting:
The code is as follows:
Curl_exec ($ ch );
$ Info = curl_getinfo ($ ch );
Echo'
';
print_r($info);
echo '
';
Returned data
The code is as follows:
Array
(
[Url] => http://www.cnblogs.com/it-cen/
[Content_type] => text/html; charset = utf-8
[Http_code] = & gt; 200
[Header_size] = & gt; 312
[Request_size] => 61
[Filetime] =>-1
[Ssl_verify_result] => 0
[Redirect_count] => 0
[Total_time] = & gt; 0.172
[Namelookup_time] = & gt; 0.016
[Connect_time] = & gt; 0.063
[Pretransfer_time] = & gt; 0.063
[Size_upload] => 0
[Size_download] => 14658 // request data size
[Speed_download] = & gt; 85220
[Speed_upload] => 0
[Download_content_length] = & gt; 14658
[Upload_content_length] => 0
[Starttransfer_time] = & gt; 0.125
[Redirect_time] => 0
[Certinfo] => Array
(
)
[Redirect_url] =>
)
2. This information is useful for debugging.For example, when cURL is captured, data capture may be incomplete due to network or other reasons. this is how we can calculate the filesize through the obtained data, and then calculate the filesize with curl_getinfo () if the download size is equal, make sure that the download is correct. Otherwise, try again.
The following is an example of image capturing:
The code 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 obtain the content
$ Res = curl_exec ($ ch );
// Obtain 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 complete data ";
}
?>
3. use the POST method to send data in cURL
The code 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 it to 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;
?>
You can use this method to simulate messages, or you can use a Irrigation robot. The idea is the same.
4. use cURL to upload files
The code is as follows:
<? Php
// Request the 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: To send a file, add the @ prefix before the file name and use the complete path.
5. cURL settings
In fact, cURL has many configuration options, which are the soul of cURL. through setopt () settings, we will summarize several common and important configuration items, it is hoped that it will be helpful for readers to use cURL in the future:
CURLOPT_AUTOREFERER: When location: redirection is enabled, the Referer: Information in the header is automatically set.
CURLOPT_COOKIESESSION: When enabled, cURL will closely pass a sessioncookie and ignore other cookies.
CURLOPT_HEADER: outputs 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: specifies the maximum number of HTTP redirection requests.
CURLOPT_COOKIE: sets the content of "cookie:" in an HTTP request. multiple cookies are followed by semicolons, followed by a space.
CURLOPT_POSTFIELDS: use the "POST" operation in HTTP to send all data to the file to be sent. add the @ prefix to the file name and use the complete path.
.......
For more configuration items, see The PHP Manual.
CURL is a common library and not exclusive to PHP.
I hope that you can learn from several classic cURL examples in this blog post.