茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    php curl請(qǐng)求信息和返回信息設(shè)置代碼實(shí)例
    來源:易賢網(wǎng) 閱讀:870 次 日期:2015-04-28 14:57:48
    溫馨提示:易賢網(wǎng)小編為您整理了“php curl請(qǐng)求信息和返回信息設(shè)置代碼實(shí)例”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了php curl請(qǐng)求信息和返回信息設(shè)置代碼實(shí)例,本文直接給出代碼實(shí)例,需要的朋友可以參考下

    在用curl抓取網(wǎng)頁(yè)內(nèi)容的時(shí)候,經(jīng)常要知道,網(wǎng)頁(yè)返回的請(qǐng)求頭信息,和請(qǐng)求的相關(guān)信息,特別是在請(qǐng)求過程中存在重定向的時(shí)候獲取請(qǐng)求返回頭信息對(duì)分析請(qǐng)求內(nèi)容很有幫助

    下面就是一個(gè)請(qǐng)求中存在重定向的例子,我們的目的是要獲取最終實(shí)際請(qǐng)求的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è)置請(qǐng)求最多重定向的次數(shù)

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//不直接輸出response

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//如果返回的response 頭部中存在Location值,就會(huì)遞歸請(qǐng)求

    $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] => [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)過遞歸請(qǐng)求后最終得到一個(gè)200的response,但是這中方式不能得到最后一次請(qǐng)求的url,也就是最終實(shí)際請(qǐng)求的url,要想得到這個(gè)url就需要遞歸的分析每次請(qǐng)求返回的response

    下面是我寫的一個(gè)獲取最后一次請(qǐng)求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í)行時(shí)間不超過3秒

    //curl_setopt($ch, CURLOPT_NOBODY, 1);//這行不能要,如果添上,那么在遇到302重定向的時(shí)候就會(huì)得不到真正的請(qǐng)求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;

    }

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:php curl請(qǐng)求信息和返回信息設(shè)置代碼實(shí)例
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)