php curl請求資訊和返回資訊設定代碼執行個體_php執行個體

來源:互聯網
上載者:User

在用curl抓取網頁內容的時候,經常要知道,網頁返回的要求標頭資訊,和請求的相關資訊,特別是在請求過程中存在重新導向的時候擷取請求返回頭資訊對分析請求內容很有協助

下面就是一個請求中存在重新導向的例子,我們的目的是要擷取最終實際請求的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';  $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);//返回response頭部資訊 curl_setopt($ch, CURLOPT_NOBODY, 1);//不返回response body內容 //curl_setopt($ch, CURLOPT_MAXREDIRS, 1);//佈建要求最多重新導向的次數 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,"</br>"; echo "<hr>"; print_r($rinfo); 

下面是輸出的結果

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: 2142149Array( [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的遞迴函式

$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);//設定curl執行時間不超過3秒   //curl_setopt($ch, CURLOPT_NOBODY, 1);//這行不能要,如果添上,那麼在遇到302重新導向的時候就會得不到真正的請求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; } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.