phpcurl函數類模擬Curl get post header refer攜帶Cookie模擬訪問來源Refer模擬UseaAgent ...
phpCurl函數類,網上很多,這裡分享一個萬能phpcurl,包含phpcurl函數類模擬Curl get post header refer攜帶Cookie模擬訪問來源Refer模擬UseaAgent
<?php /** * @author 教書先生 * @link https://blog.oioweb.cn * @date 2021年6月13日10:29:04 * @msg PHPCurl封裝的方法 */ function teacher_curl($url, $paras = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if (isset($paras['Header'])) { $Header = $paras['Header']; } else { $Header[] = "Accept:*/*"; $Header[] = "Accept-Encoding:gzip,deflate,sdch"; $Header[] = "Accept-Language:zh-CN,zh;q=0.8"; $Header[] = "Connection:close"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $Header); if (isset($paras['ctime'])) { // 連接超時 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']); } else { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); } if (isset($paras['rtime'])) { // 讀取超時 curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']); } if (isset($paras['post'])) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']); } if (isset($paras['header'])) { curl_setopt($ch, CURLOPT_HEADER, true); } if (isset($paras['cookie'])) { curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']); } if (isset($paras['refer'])) { if ($paras['refer'] == 1) { curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f='); } else { curl_setopt($ch, CURLOPT_REFERER, $paras['refer']); } } if (isset($paras['ua'])) { curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']); } else { curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); } if (isset($paras['nobody'])) { curl_setopt($ch, CURLOPT_NOBODY, 1); } curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (isset($paras['GetCookie'])) { curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($result, 0, $headerSize); //狀態碼 $body = substr($result, $headerSize); $ret = [ "Cookie" => $matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), ]; curl_close($ch); return $ret; } $ret = curl_exec($ch); if (isset($paras['loadurl'])) { $Headers = curl_getinfo($ch); if (isset($Headers['redirect_url'])) { $ret = $Headers['redirect_url']; } else { $ret = false; } } curl_close($ch); return $ret; }
使用方法
GET訪問
echo teacher_curl("https://kuaidi.jsonin.com/JT5124806639322.html");
POST訪問
echo teacher_curl("https://kuaidi.ipip5.com",[ 'post'=>[ 'dh'=>'JT5124806639322' ] ]);
或
echo teacher_curl("https://kuaidi.jsonin.com/",[ 'post'=>'dh=JT5124806639322' ]);
攜帶Cookie訪問
echo teacher_curl("https://www.xiaodoubi.com/",[ 'cookie'=>'cookie內容' ]);
模擬訪問來源Refer
echo teacher_curl("https://www.xiaodoubi.com/",[ 'refer'=>'https://www.xiaof.com' ]);
模擬UseaAgent
echo teacher_curl("https://www.xiaodoubi.com/",[ 'ua'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' ]);
文件上傳
echo teacher_curl("https://www.ipip5.com/",[ 'post'=>[ 'file'=>new CURLFile(realpath("Curl.jpg")) ] ]);
或
echo teacher_curl("https://www.ipip5.com/",[ 'post'=>new CURLFile(realpath("Curl.jpg")) ]);
獲取301跳轉地址
echo teacher_curl("https://www.xiaof.com",[ 'loadurl'=>1 ]);
設置請求頭信息
echo teacher_curl("https://www.ipip5.com/",[ 'Header'=>[ 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 accept-encoding: gzip, deflate, br accept-language: zh-CN,zh;q=0.9 cache-control: max-age=0' ] ]);
獲取請求的全部信息
echo teacher_curl("https://www.ipip5.com/today/api.php?type=json",[ 'post'=>[ 'user'=>123456, 'pwd'=>123 ], 'GetCookie'=>1 ]);
我自用的
function curl($url){ //Curl GET $ch = curl_init(); // Curl 初始化 $timeout = 30; // 超時時間:30s $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';// 偽造抓取 UA $ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240); curl_setopt($ch, CURLOPT_URL, $url);// 設置 Curl 目標 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Curl 請求有返回的值 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// 設置抓取超時時間 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// 跟蹤重定向 curl_setopt($ch, CURLOPT_REFERER, 'https://www.baidu.com/');//模擬來路 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip)); //偽造IP curl_setopt($ch, CURLOPT_USERAGENT, $ua);// 偽造ua curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// https請求 不驗證證書和hosts curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//強制協議為1.0 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//強制使用IPV4協議解析功能變數名稱 $content = curl_exec($ch); curl_close($ch);// 結束 Curl return $content;// 函數返回內容 }
$url = 'https://www.xiaodoubi.com/';
echo curl($url);
博主利用這兩個phpcurl函數做了一個線上工具:線上post線上get請求調試 https://post.jsonin.com