php curl抓取類分享
阿新 • • 發佈:2017-07-19
stream 網絡 network param als logs 獲取圖片 cat close
class UsualFunForNetWorkHelper { /*** * post請求數據 */ public static function HttpsPost($url, $data = null, $headers = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); header("Content-type: text/html; charset=utf-8"); return $output; } /*** * http的get訪問方法 */ public static function HttpGet($url, $headers = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT‘]); // 模擬用戶使用的瀏覽器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循環 //curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回 $output = curl_exec($curl); //echo ‘[‘.$output.‘]‘; return $output; } /** * 獲取圖片 * @param $url * @param $content_type * @return bool|int|mixed */ public static function HttpGetImg($url,&$content_type,&$error=‘‘) { $content_type = ‘‘; $count = 0; $e_no = 28; while($count < 10 && $e_no === 28)//網絡超時,10次重復請求 { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT‘]); // 模擬用戶使用的瀏覽器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 設置超時限制防止死循環 //curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回 $output = curl_exec($curl); $content_type = curl_getinfo($curl,CURLINFO_CONTENT_TYPE); $e_no = curl_errno($curl); curl_close($curl); $count ++; } if($e_no !== 0) { $error = $e_no; if($e_no === 28) { Log::write(‘請求圖片超時10次,放棄,url:‘.$url); } return false; } $content_type = strval($content_type); $ok_type=[ ‘application/octet-stream‘, ‘image/bmp‘, ‘image/gif‘, ‘image/jpeg‘, ‘image/png‘ ]; if(!in_array($content_type,$ok_type)) { return false; } return $output; } }
php curl抓取類分享