"About PHP" 16th bullet---File transfer tool Curl's advanced application
Let's move on to the above section, which gives you a brief introduction to the four steps of using curl, this article explains some of the advanced applications of Curl:
To obtain information about the request, we can take advantage of curl_getinfo () after Curl execution is complete:
-
- Create a new Curl resource
- $ch = Curl_init ("http://www.lampbrother.net");
- Set the URL and the appropriate options
- curl_setopt ($ch, curlopt_returntransfer,1);
- Check to see if an error has occurred
- if (!curl_errno ($ch))
- {
- $info = Curl_getinfo ($ch);
- Var_dump ($info);
- }
- Crawl the URL and pass it to the browser
- $html = curl_exec ($ch);
- Turn off the Curl resource and release the system resources
- Curl_close ($ch);
- ?>
the printed content is:
Array
' URL '=String' Http://www.lampbrother.net '(length=26)
' Content_Type '= NULL
' Http_code '=Int0
' Header_size '=Int0
' Request_size '=Int0
' Filetime '=Int0
' Ssl_verify_result '=Int0
' Redirect_count '=Int0
' Total_time '=Float0
' Namelookup_time '=Float0
' Connect_time '=Float0
' Pretransfer_time '=Float0
' Size_upload '=Float0
' Size_download '=Float0
' Speed_download '=Float0
' Speed_upload '=Float0
' Download_content_length '=Float-1
' Upload_content_length '=Float-1
' Starttransfer_time '=Float0
' Redirect_time '=Float0
' Certinfo '=Array
Empty
' Redirect_url '=String'' (length=0)
The following information is included in the returned array:
"url"//Resource Network address
"Content_Type"//content encoding
"Http_code"//http status code
size of the "header_size"//header
"Request_size"//Requested size
"FILETIME"//File creation time
"Ssl_verify_result"//ssl validation results
"Redirect_count"//Jump technology
"Total_time"//Total time-consuming
"Namelookup_time"//dns query time-consuming
"Connect_time"//Waiting for connection time
"Pretransfer_time"//Pre-transmission preparation time
"Size_upload"//size of uploaded data
size of "size_download"//Download Data
"speed_download"//download speed
"Speed_upload"//upload speed
"Download_content_length"//length of downloaded content
"Upload_content_length"//Length of uploaded content
"Starttransfer_time"//Start transfer time
"Redirect_time"//redirect time-consuming
we can even use curl to simulate the way the browser sends data by post:
let's start by creating a page that can print the post data:
-
- Var_dump ($_post);
- ?>
Create a new page to simulate the browser sending post data:
-
- $url = "http://localhost/post.php";
- $post _data = Array (
- "Author" = "Jie Li",
- "title" and "Jie Brother about PHP"
- );
- Initialize, create a new curl resource
- $ch = Curl_init ();
- Set the URL and the appropriate 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);
- Crawl the URL and pass it to the browser
- $out = curl_exec ($ch);
- Turn off the Curl resource and release the system resources
- Curl_close ($ch);
- Echo $output;
- ?>
Print out the results:
Array
' Author '=String' Jie Li ' (length=4)
' Title '=String' talking about PHP ' in Czech Republic (length=11)
we can see that the powerful curl has helped us pass the post data over, and it's a process like this:
1. Pass the post data to the post.php page
The 2.post.php page displays the post data output on the page
3.curl post.php Receive and print out the post data crawl back, output on the page!
not only can we use post to pass data, we can also upload files, essentially the same way:
curl.php
-
- $url = "http://localhost/upload.php";
- $post _data = Array (
- "title" = "Amazing!!!" ",
- "Pic" = "@d:\ Li Wenkei only glamorous photos. jpg"
- );
- Initialize, create a new curl resource
- $ch = Curl_init ();
- Set the URL and the appropriate 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);
- Crawl the URL and pass it to the browser
- $out = curl_exec ($ch);
- Turn off the Curl resource and release the system resources
- Curl_close ($ch);
- Echo $output;
- ?>
upload.php
- !--? php
-
- var_dump ($_files);
-
- ?
passed back value: Span style= "font-size:18px" >
array
' Pic ' => array
' name ' => string Li Wenkei only glamorous photos. jpg ' (length=18)
' type ' (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:\ Li Wenkei only glamorous photos. jpg"
- );
the upload needs to be noted that Add the @ symbol before the file name you want to upload!
Curl Batch processing:
Curl also has an advanced application, batch handle, which can handle multiple URL connections synchronously or asynchronously:
-
- Create a pair of curl resources
- $ch 1 = curl_init ();
- $ch 2 = Curl_init ();
- Set the URL and the appropriate options
- curl_setopt ($ch 1, Curlopt_url, "http://www.li-jie.me/");
- curl_setopt ($ch 1, curlopt_header, 0);
- curl_setopt ($ch 2, Curlopt_url, "http://www.lampbrother.net/");
- curl_setopt ($ch 2, Curlopt_header, 0);
- Create a batch curl handle
- $MH = Curl_multi_init ();
- Add 2 Handles
- Curl_multi_add_handle ($MH, $ch 1);
- Curl_multi_add_handle ($MH, $ch 2);
- $running =null;
- Executing a batch Handle
- do {
- Usleep (10000);
- Curl_multi_exec ($MH, $running);
- } while ($running > 0);
- Close all handles
- Curl_multi_remove_handle ($MH, $ch 1);
- Curl_multi_remove_handle ($MH, $ch 2);
- Curl_multi_close ($MH);
- ?>
$running collects page content from http://www.li-jie.me and http://www.lampbrother.net, enabling batch processing of multiple URLs!
You See, after the collection site to abandon file_get_contents and fopen bar, put our strong curl use up, will help your Web application to enhance a lot!