PHP cURL初始化和執(zhí)行方法入門級代碼
更新時間:2015年05月28日 15:20:01 投稿:junjie
這篇文章主要介紹了PHP cURL初始化和執(zhí)行方法入門級代碼,本文直接給出代碼示例,代碼中包含詳細注釋,需要的朋友可以參考下
這個是采集基礎(chǔ),最好熟悉一下
$ch = curl_init();
# 設(shè)定url和把結(jié)果返回,是否返回頭部
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);
# cookie文件設(shè)定
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file);
# 額外頭部
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0'));
# 設(shè)定post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);
# 連接、執(zhí)行過期時間
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
# 是否跟隨301 302
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
# refer
curl_setopt($this->ch, CURLOPT_REFERER, $refer);
# http版本和端口重用設(shè)置
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);
# 支持https
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
# 如果需要進行毫秒超時,需要增加:
curl_setopt($this->ch, CURLOPT_NOSIGNAL, 1);
# 執(zhí)行
$response = curl_exec($ch);
if(curl_errno($ch)){
curl_error($ch);
exit();
}
curl_close($ch);
相關(guān)文章
Zend Framework實現(xiàn)將session存儲在memcache中的方法
這篇文章主要介紹了Zend Framework實現(xiàn)將session存儲在memcache中的方法,結(jié)合實例形式分析了Zend Framework框架下將session存儲在memcache的實現(xiàn)技巧,需要的朋友可以參考下2016-03-03
smarty內(nèi)置函數(shù)capture用法分析
這篇文章主要介紹了smarty內(nèi)置函數(shù)capture用法,實例分析了capture的三種常見用法,需要的朋友可以參考下2015-01-01
PHP使用fopen與file_get_contents讀取文件實例分享
這篇文章主要介紹了PHP使用fopen與file_get_contents讀取文件實例分享的相關(guān)資料,需要的朋友可以參考下2016-03-03

