《PHP實(shí)戰(zhàn):PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù))》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù)),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用通過curl_setopt()函數(shù)可以方便快捷的抓取網(wǎng)頁(采集很方便大笑),curl_setopt 是PHP的一個擴(kuò)展庫
????
使用條件:需要在php.ini 中配置開啟.(PHP 4 >= 4.0.2)
?????? //取消下面的注釋
PHP應(yīng)用extension=php_curl.dll
PHP應(yīng)用在Linux下面,需要重新編譯PHP了,編譯時,你需要打開編譯參數(shù)――在configure命令上加上“Cwith-curl” 參數(shù).
PHP應(yīng)用1、 一個抓取網(wǎng)頁的簡單案例:
PHP應(yīng)用
[php] view plain copy print?
// 創(chuàng)建一個新cURL資源
$ch = curl_init();
// 設(shè)置URL和相應(yīng)的選項(xiàng)
curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// 抓取URL并把它傳遞給瀏覽器
curl_exec($ch);
//關(guān)閉cURL資源,并且釋放系統(tǒng)資源
curl_close($ch);
PHP應(yīng)用2、POST數(shù)據(jù)案例:
PHP應(yīng)用
[php] view plain copy print?
// 創(chuàng)建一個新cURL資源
$ch = curl_init();
$data = 'phone='. urlencode($phone);
// 設(shè)置URL和相應(yīng)的選項(xiàng)
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);
//關(guān)閉cURL資源,并且釋放系統(tǒng)資源
curl_close($ch);
PHP應(yīng)用3、關(guān)于SSL和Cookie
PHP應(yīng)用關(guān)于SSL也就是HTTPS協(xié)議,你只需要把CURLOPT_URL連接中的http://變成https://就可以了.當(dāng)然,還有一個參數(shù)叫CURLOPT_SSL_VERIFYHOST可以設(shè)置為驗(yàn)證站點(diǎn).
PHP應(yīng)用關(guān)于Cookie,你需要了解下面三個參數(shù):
PHP應(yīng)用PS:新浪微博登陸API部分截取(部分我增加了點(diǎn)注釋,全當(dāng)參數(shù)翻譯下.哈哈) 有興趣的自己研究,自己挪為己用.嘿嘿
PHP應(yīng)用
[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);//在發(fā)起連接前等待的時間,如果設(shè)置為0,則無限等待
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//設(shè)置cURL允許執(zhí)行的最長秒數(shù)
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)輸出
curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP請求頭中"Accept-Encoding: "的值.支持的編碼有"identity","deflate"和"gzip".如果為空字符串"",請求頭會發(fā)送所有支持的編碼類型.
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用后cURL將終止從服務(wù)端進(jìn)行驗(yàn)證
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//第一個是cURL的資源句柄,第二個是輸出的header數(shù)據(jù)
curl_setopt($ci, CURLOPT_HEADER, FALSE);//啟用時會將頭文件的信息作為數(shù)據(jù)流輸出
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;
}
PHP應(yīng)用以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持維易PHP!
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2303.html