本文主要對PHP的CURL方法curl_setopt()函數案例進行介紹:1.抓取網頁的簡單案例;2.POST資料案例
通過curl_setopt()函數可以方便快捷的抓取網頁(採集很方便大笑),curl_setopt 是PHP的一個擴充庫
使用條件:需要在php.ini 中配置開啟。(PHP 4 >= 4.0.2)
//取消下面的注釋
extension=php_curl.dll
在Linux下面,需要重新編譯PHP了,編譯時間,你需要開啟編譯參數——在configure命令上加上“–with-curl” 參數。
1、 一個抓取網頁的簡單案例:
[php] view plain copy print?// 建立一個新cURL資源 $ch = curl_init(); // 設定URL和相應的選項 curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/"); curl_setopt($ch, CURLOPT_HEADER, false); // 抓取URL並把它傳遞給瀏覽器 curl_exec($ch); //關閉cURL資源,並且釋放系統資源 curl_close($ch);
2、POST資料案例:
[php] view plain copy print?// 建立一個新cURL資源 $ch = curl_init(); $data = 'phone='. urlencode($phone); // 設定URL和相應的選項 curl_setopt($ch, CURLOPT_URL, "http://www.post.com/"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 抓取URL並把它傳遞給瀏覽器 curl_exec($ch); //關閉cURL資源,並且釋放系統資源 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部分截取(部分我增加了點注釋,全當參數翻譯下。哈哈) 有興趣的自己研究,自己挪為己用。嘿嘿
[php] view plain copy print?/** * Make an HTTP request * * @return string API results * @ignore */ function http($url, $method, $postfields = NULL, $headers = array()) { $this->http_info = array(); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//讓cURL自己判斷使用哪個版本 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//在HTTP請求中包含一個"User-Agent: "頭的字串。 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//在發起串連前等待的時間,如果設定為0,則無限等待 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//設定cURL允許執行的最長秒數 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)輸出 curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP要求標頭中"Accept-Encoding: "的值。支援的編碼有"identity","deflate"和"gzip"。如果為空白字串"",要求標頭會發送所有支援的編碼類別型。 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用後cURL將終止從服務端進行驗證 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//第一個是cURL的資源控制代碼,第二個是輸出的header資料 curl_setopt($ci, CURLOPT_HEADER, FALSE);//啟用時會將標頭檔的資訊作為資料流輸出 switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); $this->postdata = $postfields; } break; case 'DELETE': curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($postfields)) { $url = "{$url}?{$postfields}"; } } if ( isset($this->access_token) && $this->access_token ) $headers[] = "Authorization: OAuth2 ".$this->access_token; $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR']; curl_setopt($ci, CURLOPT_URL, $url ); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers ); curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE ); $response = curl_exec($ci); $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); $this->url = $url; if ($this->debug) { echo "=====post data======\r\n"; var_dump($postfields); echo '=====info====='."\r\n"; print_r( curl_getinfo($ci) ); echo '=====$response====='."\r\n"; print_r( $response ); } curl_close ($ci); return $response; }
以上就是本文的全部內容,希望對大家的學習有所協助。