本篇內(nèi)容介紹了“php curl請求信息和返回信息設(shè)置的實現(xiàn)方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

在用curl抓取網(wǎng)頁內(nèi)容的時候,經(jīng)常要知道,網(wǎng)頁返回的請求頭信息,和請求的相關(guān)信息,特別是在請求過程中存在重定向的時候獲取請求返回頭信息對分析請求內(nèi)容很有幫助
下面就是一個請求中存在重定向的例子,我們的目的是要獲取最終實際請求的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內(nèi)容 //curl_setopt($ch, CURLOPT_MAXREDIRS, 1);//設(shè)置請求最多重定向的次數(shù) 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);
下面是輸出的結(jié)果
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 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 ( ))
可以看到,經(jīng)過遞歸請求后最終得到一個200的response,但是這中方式不能得到最后一次請求的url,也就是最終實際請求的url,要想得到這個url就需要遞歸的分析每次請求返回的response
下面是我寫的一個獲取最后一次請求url的遞歸函數(shù)
$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);//設(shè)置curl執(zhí)行時間不超過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;
}“php curl請求信息和返回信息設(shè)置的實現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)站名稱:phpcurl請求信息和返回信息設(shè)置的實現(xiàn)方法-創(chuàng)新互聯(lián)
文章URL:http://chinadenli.net/article24/hjsce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、微信公眾號、服務(wù)器托管、全網(wǎng)營銷推廣、網(wǎng)站制作、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容