[Jie GE talk about PHP] 16th bullet --- advanced application of file transfer tool cURL. next we will talk about the above content. the above briefly introduces the four steps of using curl, this article will explain some advanced applications of curl: to obtain the request information, we can use curl_getinfo (): & lt ;? Php/[Jie GE talk about PHP] 16th bullet --- advanced application of file transfer tool cURL
Next, let's talk about the above content. the above section briefly introduces the four steps for using curl. This article will explain some advanced applications of curl:
Obtain the request information. after the curl is executed, use curl_getinfo ():
-
- // Create a new cURL resource
- $ Ch = curl_init ("http://www.lampbrother.net ");
- // Set the URL and corresponding options
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- // Check for any errors
- If (! Curl_errno ($ ch ))
- {
- $ Info = curl_getinfo ($ ch );
- Var_dump ($ info );
- }
- // Capture the URL and pass it to the browser
- $ Html = curl_exec ($ ch );
- // Disable cURL resources and release system resources
- Curl_close ($ ch );
- ?>
The printed content is:
Array
'URL' => string 'http: // www.lampbrother.net'(Length = 26)
'Content _ type' => null
'Http _ code' => int 0
'Header _ size' => int 0
'Request _ size' => int 0
'Filetime' => int 0
'SSL _ verify_result '=> int 0
'Redirect _ count' => int 0
'Total _ time' => float 0
'Namelookup _ time' => float 0
'Connect _ time' => float 0
'Pretransfer _ time' => float 0
'Size _ upload' => float 0
'Size _ download' => float 0
'Speed _ download' => float 0
'Speed _ upload' => float 0
'Download _ content_length '=> float-1
'Upload _ content_length '=> float-1
'Starttransfer _ time' => float 0
'Redirect _ time' => float 0
'Certinfo' =>Array
Empty
'Redirect _ url' => string''(Length = 0)
The returned array contains the following information:
"Url" // resource network address
"Content_type" // content encoding
"Http_code" // HTTP status code
"Header_size" // header size
"Request_size" // request size
"Filetime" // file creation time
"Ssl_verify_result" // SSL verification result
"Redirect_count" // jump Technology
"Total_time" // total time consumed
"Namelookup_time" // DNS query time
"Connect_time" // waiting for connection time
"Pretransfer_time" // pre-transmission preparation time
"Size_upload" // size of the uploaded data
"Size_download" // size of the downloaded data
"Speed_download" // download speed
"Speed_upload" // upload speed
"Download_content_length" // The length of the downloaded content
"Upload_content_length" // length of the uploaded content
"Starttransfer_time" // start time of transmission
"Redirect_time" // time consumed by redirection
We can even use curl to simulate the browser to send data in POST mode:
Let's first create a page that can print POST data:
-
- Var_dump ($ _ POST );
- ?>
Create a new page to simulate a browser to send POST data:
-
- $ Url = "http: // localhost/post. php ";
- $ Post_data = array (
- "Author" => "Li Jie ",
- "Title" => "talking about PHP"
- );
- // Initialize and create a new cURL resource
- $ Ch = curl_init ();
- // Set the URL and corresponding options
- Curl_setopt ($ ch, CURLOPT_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ ch, CURLOPT_POST, 1 );
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
- // Capture the URL and pass it to the browser
- $ Out = curl_exec ($ ch );
- // Disable cURL resources and release system resources
- Curl_close ($ ch );
- Echo $ output;
- ?>
The output is as follows:
Array
'Author' => string 'Jet Li'(Length = 4)
'Title' => string' php'(Length = 11)
We can see that the powerful curl has helped us pass the post data. it is like this process:
1. pass post data to the post. php page
2. the post. php page displays the post data output on the page.
3. curl captures the post data received and printed by post. php and outputs the data on the page!
We can not only use post to transmit data, but also Upload files in the same way:
Curl. php
-
- $ Url = "http: // localhost/upload. php ";
- $ Post_data = array (
- "Title" => "amazing !!! ",
- "Pic" => "@ d: \ 文 .jpg"
- );
- // Initialize and create a new cURL resource
- $ Ch = curl_init ();
- // Set the URL and corresponding options
- Curl_setopt ($ ch, CURLOPT_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ ch, CURLOPT_POST, 1 );
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
- // Capture the URL and pass it to the browser
- $ Out = curl_exec ($ ch );
- // Disable cURL resources and release system resources
- Curl_close ($ ch );
- Echo $ output;
- ?>
Upload. php
-
- Var_dump ($ _ FILES );
- ?>
Passed value:
Array
'Pic '=>Array
'Name' => string 'Lee Venucia. JPG'(Length = 18)
'Type' => string 'application/octet-stream'(Length = 24)
'Tmp _ name' => string 'f: \ LAMPBrother \ Environmental \ wamp_32 \ tmp \ php9A73. tmp'(Length = 52)
'Error' => int 0
'Size' => int 0
- $ Post_data = array (
- "Title" => "amazing !!! ",
- "Pic" => "@ d: \ 文 .jpg"
- );
Note that the @ symbol must be added before the file name to be uploaded!
CURL batch processing:
CURL also has an advanced application, batch processing handle, which can process multiple URL connections synchronously or asynchronously:
-
- // Create a cURL resource
- $ Response = curl_init ();
- $ Ch2 = curl_init ();
- // Set the URL and corresponding options
- Curl_setopt ($ scheme, CURLOPT_URL, "http://www.li-jie.me /");
- Curl_setopt ($ scheme, CURLOPT_HEADER, 0 );
- Curl_setopt ($ ch2, CURLOPT_URL, "http://www.lampbrother.net /");
- Curl_setopt ($ ch2, CURLOPT_HEADER, 0 );
- // Create a cURL handle for batch processing
- $ Mh = curl_multi_init ();
- // Add two handles
- Curl_multi_add_handle ($ mh, $ handle );
- Curl_multi_add_handle ($ mh, $ ch2 );
- $ Running = null;
- // Execute the batch processing handle
- Do {
- Usleep (10000 );
- Curl_multi_exec ($ mh, $ running );
- } While ($ running> 0 );
- // Close all handles
- Curl_multi_remove_handle ($ mh, $ handle );
- Curl_multi_remove_handle ($ mh, $ ch2 );
- Curl_multi_close ($ mh );
- ?>
$ Running collects page content from http://www.li-jie.me and http://www.lampbrother.net for batch processing of multiple URLs!
As you can see, the collection website will discard file_get_contents and fopen in the future. using our powerful cURL will help you increase the color of your web applications!