php curl使用 常用操作

來源:互聯網
上載者:User

標籤:處理   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使用 常用操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.