国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法

瀏覽:152日期:2022-09-08 11:35:10

導(dǎo)讀:

curl請(qǐng)求時(shí)添加請(qǐng)求頭信息可以模擬真人操作,不容易被當(dāng)成是爬蟲(chóng)機(jī)器人(采集),從而可以繞過(guò)Incapsula等安全驗(yàn)證機(jī)制。

1、首先使用瀏覽器(示例使用的是火狐瀏覽器)訪問(wèn)接口網(wǎng)址,使用F12調(diào)試,查看請(qǐng)求頭信息,如下:

php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法

2、實(shí)現(xiàn)代碼:

<?php/** * 開(kāi)始訪問(wèn)請(qǐng)求 * @param $url * @return bool|string */function fetch_url($url) {$header = FormatHeader($url);$useragent = ’Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0’;$timeout= 120;$ch = curl_init($url);curl_setopt($ch, CURLOPT_FAILONERROR, true);//設(shè)置請(qǐng)求頭信息curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//不取得返回頭信息curl_setopt($ch, CURLOPT_HEADER, 0);// 關(guān)閉https驗(yàn)證curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );curl_setopt($ch, CURLOPT_ENCODING, '' );curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );curl_setopt($ch, CURLOPT_AUTOREFERER, true );curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );curl_setopt($ch, CURLOPT_USERAGENT, $useragent);$content = curl_exec($ch);if(curl_errno($ch)){echo ’Error:’ . curl_error($ch);}else{return $content; }curl_close($ch);} //添加請(qǐng)求頭function FormatHeader($url){ // 解析url $temp = parse_url($url); $query = isset($temp[’query’]) ? $temp[’query’] : ’’; $path = isset($temp[’path’]) ? $temp[’path’] : ’/’; $header = array ( 'POST {$path}?{$query} HTTP/1.1', 'Host: {$temp[’host’]}', 'Referer: http://{$temp[’host’]}/', 'Content-Type: text/xml; charset=utf-8', ’Accept: application/json, text/javascript, */*; q=0.01’, ’Accept-Encoding:gzip, deflate, br’, ’Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2’, ’Connection:keep-alive’, ’User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0’, ’X-Requested-With: XMLHttpRequest’, ); return $header;}?>

3、調(diào)用示例:

<?php//lcg_value() 返回范圍為 (0, 1) 的一個(gè)偽隨機(jī)數(shù)$url='http://www.xxx.com/getdata.php?v='.lcg_value();//訪問(wèn)網(wǎng)址$html = fetch_url($url);

到此這篇關(guān)于php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法的文章就介紹到這了,更多相關(guān)php的curl請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 崇仁县| 克拉玛依市| 龙井市| 三门峡市| 贵南县| 卓资县| 镇江市| 恭城| 张家口市| 临泉县| 公主岭市| 东丽区| 伊通| 资兴市| 中阳县| 济宁市| 寿阳县| 尉犁县| 合江县| 寿宁县| 泗洪县| 弥勒县| 大石桥市| 富宁县| 玉门市| 大洼县| 鸡泽县| 牡丹江市| 修水县| 龙江县| 固安县| 炎陵县| 常宁市| 射阳县| 洪泽县| 彝良县| 黄山市| 清水县| 额尔古纳市| 南宫市| 开鲁县|