php模擬客戶端請求,抓取網(wǎng)站或者接口數(shù)據(jù)

  /**
   * 模擬客戶端請求,抓取網(wǎng)站或者接口數(shù)據(jù)
   * 以抓取某視頻網(wǎng)站的播放地址為例,首先我是通過fiddler抓包找到該視頻網(wǎng)站獲    
   * 取數(shù)據(jù)的接口和請求參數(shù)類型的,
   * 但是直接模擬請求提示客戶端版本太低,所以我就復(fù)制了header請求參數(shù)
  **/
   public function play_link($video_id){
        //該網(wǎng)站的接口地址;
        $url = 'http://xx.com/video/getVideoPlayLinkByVideoId';

        //模擬header內(nèi)容
        $header = array(
            'Host: web.tv',
            'Connection: keep-alive',
            'Content-Length: 14',
            'Accept: application/json, text/plain, */*',
            'Origin: http://www.tv',
            'clientVersion: 0.1.0',
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
            'clientType: web',
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'Referer: http://www.tv/',
            'Accept-Encoding: gzip, deflate',
            'Accept-Language: zh-CN,zh;q=0.9',
            'Cookie: JSESSIONID=A895974885A1D0ED9CC91C84C73FC074'
        );

        //post請求參數(shù)
        $content = array(
            'videoId' => $video_id
        );

        //curl模擬提交
        $response = self::tocurl($url, $header, $content);
        $response  = json_decode($response,true);
        return $response;
    }

    /**
     * curl提交數(shù)據(jù)
     * @param String $url     請求的地址
     * @param Array  $header  自定義的header數(shù)據(jù)
     * @param Array  $content POST的數(shù)據(jù)
     * @return String
     */
    function tocurl($url, $header, $content){
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 從證書中檢查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            die($error);
        }
        curl_close($ch);
        return $response;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容