標籤:處理 false tran pos tput function blog catch exe
1. http Get
簡單的只需要 這四行 就
$ch = curl_init ();curl_setopt ( $ch, CURLOPT_URL, "http://site" );$output = curl_exec($ch);curl_close ( $ch );
複雜的
public static function curlGet($url,$cookiefile,$header=null){ try{ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //不知道如何看發出去的資料是什麼樣子的 使用這句話 可通過抓包工具查看到 // 需要抓包工具配合使用 curl_setopt($ch,CURLOPT_PROXY,‘192.168.2.221:8889‘); if($header){// curl_setopt ( $ch, CURLOPT_HEADER, 1 ); curl_setopt($ch, CURLOPT_HTTPHEADER, $header);// curl_setopt ( $ch, CURLOPT_COOKIE,$header[‘Cookie‘] );// curl_setopt ( $ch, CURLOPT_COOKIESESSION,$header[‘Cookie‘] ); } else{ curl_setopt ( $ch, CURLOPT_HEADER, 0 ); } if($cookiefile){ curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookiefile ); // 讀取cookie curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookiefile ); // 設定Cookie資訊儲存在指定的檔案中 } $output = curl_exec($ch);
// 擷取curl 資訊 $information = curl_getinfo($ch); curl_close ( $ch ); }catch(\Exception $e){ print_r($e->getMessage()); } return $output; }
2. http Post
public static function curlPost($url,$data,$cookiefile=null,$header=null){ try{ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt($ch,CURLOPT_PROXY,‘192.168.2.221:8889‘); if($header){ curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } else{ curl_setopt ( $ch, CURLOPT_HEADER, 0 ); } if($cookiefile){
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookiefile ); // 讀取cookie curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookiefile ); // 設定Cookie資訊儲存在指定的檔案中 } //!!!注意data的格式 curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data); //設定響應逾時時間 curl_setopt($ch, CURLOPT_TIMEOUT, 120); $output = curl_exec($ch); if($output === false){ if(curl_errno($ch) == CURLE_OPERATION_TIMEDOUT){ //處理邏輯 } } $information = curl_getinfo($ch); curl_close ( $ch ); }catch(\Exception $e){ print_r($e->getMessage()); } return $output; }
3. https Get (未完待續)
4. https Post(未完待續)
5. curl 使用過程中 發現不足的地方(未完待續)
php curl使用 常用操作