PHP的CURL方法curl_setopt()函數案例介紹(抓取網頁,POST數據)

 通過curl_setopt()函數可以方便快捷的抓取網頁(採集很方便大笑),curl_setopt 是php的一個擴展庫

     使用條件:需要在php.ini 中配置開啓。(PHP 4 >= 4.0.2)
       //取消下面的註釋
extension=php_curl.dll

      在Linux下面,需要重新編譯PHP了,編譯時,你需要打開編譯參數——在configure命令上加上“–with-curl” 參數。

1、 一個抓取網頁的簡單案例:

   

  1. // 創建一個新cURL資源  
  2. $ch = curl_init();  
  3.   
  4. // 設置URL和相應的選項  
  5. curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");  
  6. curl_setopt($ch, CURLOPT_HEADER, false);  
  7.   
  8. // 抓取URL並把它傳遞給瀏覽器  
  9. curl_exec($ch);  
  10.   
  11. //關閉cURL資源,並且釋放系統資源  
  12. curl_close($ch);  
2、POST數據案例:

  1. // 創建一個新cURL資源  
  2. $ch = curl_init();  
  3. $data = 'phone='. urlencode($phone);  
  4. // 設置URL和相應的選項  
  5. curl_setopt($ch, CURLOPT_URL, "http://www.post.com/");  
  6. curl_setopt($ch, CURLOPT_POST, 1);  
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  8. // 抓取URL並把它傳遞給瀏覽器  
  9. curl_exec($ch);  
  10.   
  11. //關閉cURL資源,並且釋放系統資源  
  12. curl_close($ch);  

3、關於SSL和Cookie

關於SSL也就是HTTPS協議,你只需要把CURLOPT_URL連接中的http://變成https://就可以了。當然,還有一個參數叫CURLOPT_SSL_VERIFYHOST可以設置爲驗證站點。
關於Cookie,你需要了解下面三個參數:
CURLOPT_COOKIE,在當面的會話中設置一個cookie
CURLOPT_COOKIEJAR,當會話結束的時候保存一個Cookie
CURLOPT_COOKIEFILE,Cookie的文件。

PS:新浪微博登陸API部分截取(部分我增加了點註釋,全當參數翻譯下。哈哈) 有興趣的自己研究,自己挪爲己用。嘿嘿

  1. /** 
  2.      * Make an HTTP request 
  3.      * 
  4.      * @return string API results 
  5.      * @ignore 
  6.      */  
  7.     function http($url$method$postfields = NULL, $headers = array()) {  
  8.         $this->http_info = array();  
  9.         $ci = curl_init();  
  10.         /* Curl settings */  
  11.         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//讓cURL自己判斷使用哪個版本  
  12.         curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//在HTTP請求中包含一個"User-Agent: "頭的字符串。  
  13.         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//在發起連接前等待的時間,如果設置爲0,則無限等待  
  14.         curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//設置cURL允許執行的最長秒數  
  15.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)輸出  
  16.         curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP請求頭中"Accept-Encoding: "的值。支持的編碼有"identity","deflate"和"gzip"。如果爲空字符串"",請求頭會發送所有支持的編碼類型。  
  17.         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用後cURL將終止從服務端進行驗證  
  18.         curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this'getHeader'));//第一個是cURL的資源句柄,第二個是輸出的header數據  
  19.         curl_setopt($ci, CURLOPT_HEADER, FALSE);//啓用時會將頭文件的信息作爲數據流輸出  
  20.   
  21.         switch ($method) {  
  22.             case 'POST':  
  23.                 curl_setopt($ci, CURLOPT_POST, TRUE);  
  24.                 if (!empty($postfields)) {  
  25.                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);  
  26.                     $this->postdata = $postfields;  
  27.                 }  
  28.                 break;  
  29.             case 'DELETE':  
  30.                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');  
  31.                 if (!empty($postfields)) {  
  32.                     $url = "{$url}?{$postfields}";  
  33.                 }  
  34.         }  
  35.   
  36.         if ( isset($this->access_token) && $this->access_token )  
  37.             $headers[] = "Authorization: OAuth2 ".$this->access_token;  
  38.   
  39.         $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];  
  40.         curl_setopt($ci, CURLOPT_URL, $url );  
  41.         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );  
  42.         curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );  
  43.   
  44.         $response = curl_exec($ci);  
  45.         $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);  
  46.         $this->http_info = array_merge($this->http_info, curl_getinfo($ci));  
  47.         $this->url = $url;  
  48.   
  49.         if ($this->debug) {  
  50.             echo "=====post data======\r\n";  
  51.             var_dump($postfields);  
  52.   
  53.             echo '=====info====='."\r\n";  
  54.             print_r( curl_getinfo($ci) );  
  55.   
  56.             echo '=====$response====='."\r\n";  
  57.             print_r( $response );  
  58.         }  
  59.         curl_close ($ci);  
  60.         return $response;  
  61.     }
發佈了11 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章