当前位置:Gxlcms > PHP教程 > phpcurl请求信息和返回信息设置代码实例_PHP

phpcurl请求信息和返回信息设置代码实例_PHP

时间:2021-07-01 10:21:17 帮助过:23人阅读

在用curl抓取网页内容的时候,经常要知道,网页返回的请求头信息,和请求的相关信息,特别是在请求过程中存在重定向的时候获取请求返回头信息对分析请求内容很有帮助

下面就是一个请求中存在重定向的例子,我们的目的是要获取最终实际请求的url地址

  1. $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';
  2. $ch=curl_init();
  3. curl_setopt($ch, CURLOPT_URL, $url);
  4. //curl_setopt($ch, CURLOPT_POST, 1);
  5. //curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  6. curl_setopt($ch, CURLOPT_HEADER, 1);//返回response头部信息
  7. curl_setopt($ch, CURLOPT_NOBODY, 1);//不返回response body内容
  8. //curl_setopt($ch, CURLOPT_MAXREDIRS, 1);//设置请求最多重定向的次数
  9. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//不直接
输出response curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//如果返回的response 头部中存在Location值,就会递归请求 $content=curl_exec($ch); $rinfo=curl_getinfo($ch); echo $content,"
"; echo "
"; print_r($rinfo);

下面是输出的结果

  1. HTTP/1.1 200 OKServer: nginxDate: Sat, 22 Dec 2012 06:17:44 GMTContent-Type: application/vnd.android.package-archiveConnection: closeLast-Modified: Mon, 03 Dec 2012 16:00:00 GMTExpires: Tue, 03 Dec 2013 16:00:00 GMTCache-Control: max-age=31536000Content-Length: 2142149
  2. 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.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 ( ))

可以看到,经过递归请求后最终得到一个200的response,但是这中方式不能得到最后一次请求的url,也就是最终实际请求的url,要想得到这个url就需要递归的分析每次请求返回的response

下面是我写的一个获取最后一次请求url的递归函数

  1. $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';
  2. [php] view plaincopy
  3. $realUrl=getRedirectLocation($url);
  4. echo "<br>--->",$realUrl;
  5. function getRedirectLocation($url){
  6. $realUrl=$url;
  7. echo $url,"<br>";
  8. $ch=curl_init();
  9. curl_setopt($ch, CURLOPT_URL, $url);
  10. curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 3);//设置curl执行时间不超过3秒
  11. //curl_setopt($ch, CURLOPT_NOBODY, 1);//这行不能要,如果添上,那么在遇到302重定向的时候就会得不到真正的请求url
  12. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  13. $content=curl_exec($ch);
  14. //echo $content;
  15. $rinfo=curl_getinfo($ch);
  16. $matches=array();
  17. if(preg_match('/Location:\s+?(.+?)\s+?/', $content,$matches)){
  18. //echo $matches[1],"<br>";
  19. unset($content);
  20. $realUrl=getRedirectLocation($matches[1]);
  21. }
  22. if(isset($content)){
  23. unset($content);
  24. }
  25. return $realUrl;
  26. }

人气教程排行