This article mainly introduced the PHP Curl request information and the return information Setup code instance, this article directly gives the code example, needs the friend to be possible to refer to under
When crawling Web content with curl, it is often important to know that the request header information returned by the Web page, and the information requested, especially when there is redirection in the request process, getting the request return header information is helpful for analyzing the content of the request.
Here is an example of a redirect in a request, our goal is to get the URL address of the final actual request
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
$url = ' http://www.appchina.com/market/r/489267/com.appshare.android.ilisten.vapk?c=aplus.direct&uid= Gaj9cqeu1tlyzxsxn-ab4raanvfl6t6bj-vj0ribs&p=aplus.detail&m=redirect '; $ch =curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $params); curl_setopt ($ch, Curlopt_header, 1);/return response header information curl_setopt ($ch, curlopt_nobody, 1);/do not return response body content//curl_ Setopt ($ch, Curlopt_maxredirs, 1)//Set the number of times the request was requested to be redirected curl_setopt ($ch, curlopt_returntransfer,1);/no direct output response Curl_ Setopt ($ch, curlopt_followlocation,1); If there is a location value in the returned response head, the recursive request $content =curl_exec ($ch); $rinfo =curl_getinfo ($ch); Echo $content, "</br>"; echo " |
Here is the result of the output
?
| 1 2 |
http/1.1 OKServer:nginxDate:Sat, Dec 06:17:44 gmtcontent-type:application/vnd.android.package-archiveconnection:c Loselast-modified:mon, Dec 16:00:00 gmtexpires:tue, Dec 2013 16:00:00 Gmtcache-control:max-age=31536000conte nt-length:2142149 Array ([url] => http://www.d.appchina.com/McDonald/r/489267/com.appshare.android.ilisten.vapk ? c=aplus.direct&uid=gaj9cqeu1tlyzxsxn-ab4raanvfl6t6bj-vj0ribs&p=aplus.detail&m=redirect [Content_ Type] => application/vnd.android.package-archive [Http_code] => [header_size] => 289 [request_size] => 19 6 [FILETIME] =>-1 [Ssl_verify_result] => 0 [Redirect_count] => 0 [total_time] => 0.171621 [Namelookup_time] = > 0.135256 [connect_time] => 0.152913 [pretransfer_time] => 0.152916 [size_upload] => 0 [size_download] => 0 [Speed_download] => 0 [speed_upload] => 0 [Download_content_length] => 2142149 [upload_content_length] => 0 [starttransfer_time] => 0.171582 [redirect_ TIME] => 0 [certinfo] => Array ()) |
As you can see, after a recursive request, you end up with a 200 response, but this is not the way to get the last request URL, which is the final actual request URL, to want to get this URL requires a recursive analysis of each request returned response
Here's a recursive function I wrote to get the last request URL
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|