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
$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 most redirected
curl_setopt ($ch, curlopt_returntransfer,1); Response
curl_setopt ($ch, curlopt_followlocation,1) is not directly exported;//If location value exists in the returned response header, it is recursively requested
$content =curl_exec ($ch);
$rinfo =curl_getinfo ($ch);
Echo $content, "</br>";
echo "
Here is the result of the output
http/1.1 OKServer:nginxDate:Sat, Dec 06:17:44 gmtcontent-type:application/vnd.android.package-archiveconn Ection:closelast-modified:mon, Dec 16:00:00 gmtexpires:tue, Dec 2013 16:00:00 gmtcache-control:max-age=3153 6000content-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] => 200 [ Header_size] => 289 [request_size] => 196 [FILETIME] =>-1 [Ssl_verify_result] => 0 [Redirect_count] => 0 [ Total_time] => 0.171621 [namelookup_time] => 0.135256 [connect_time] => 0.152913 [pretransfer_time] => 0.1529 [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
$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 ';
[PHP] View plaincopy $realUrl =getredirectlocation ($url);
echo "</br>--->", $realUrl;
function Getredirectlocation ($url) {$realUrl = $url;
echo $url, "</br>";
$ch =curl_init ();
curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, 1); curl_setopt ($ch, Curlopt_timeout, 3);/Set curl execution time of not more than 3 seconds//curl_setopt ($ch,
Curlopt_nobody, 1);//This line can not be, if added, then encountered 302 redirects will not be able to get the real request URL curl_setopt ($ch, curlopt_returntransfer,1);
$content =curl_exec ($ch);
Echo $content;
$rinfo =curl_getinfo ($ch);
$matches =array (); if (preg_match) ('/location:\s+? +?)
\s+?/', $content, $matches)) {//echo $matches [1], "</br>";
Unset ($content);
$realUrl =getredirectlocation ($matches [1]);
} if (Isset ($content)) {unset ($content);
return $REALURL;}