php遠(yuǎn)程請求CURL實例教程(爬蟲、保存登錄狀態(tài))
cURL
cURL可以使用URL的語法模擬瀏覽器來傳輸數(shù)據(jù),因為它是模擬瀏覽器,因此它同樣支持多種協(xié)議,F(xiàn)TP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等協(xié)議都可以很好的支持,包括一些:HTTPS認(rèn)證,HTTP POST方法,HTTP PUT方法,F(xiàn)TP上傳,keyberos認(rèn)證,HTTP上傳,代理服務(wù)器,cookies,用戶名/密碼認(rèn)證,下載文件斷點續(xù)傳,上傳文件斷點續(xù)傳,http代理服務(wù)器管道,甚至它還支持IPv6,scoket5代理服務(wù)器,通過http代理服務(wù)器上傳文件到FTP服務(wù)器等等。
本文主要介紹的是php遠(yuǎn)程請求CURL(爬蟲、保存登錄狀態(tài))的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧
GET案例
/**
* curl_get
* @param $url
* @param null $param
* @param null $options
* @return array
*/
function curl_get($url,$param = null,$options = null){
if(empty($options)){
$options = array(
'timeout' => 30,// 請求超時
'header' => array(),
'cookie' => '',// cookie字符串,瀏覽器直接復(fù)制即可
'cookie_file' => '',// 文件路徑,并要有讀寫權(quán)限的
'ssl' => 0,// 是否檢查https協(xié)議
'referer' => null
);
}else{
empty($options['timeout']) && $options['timeout'] = 30;
empty($options['ssl']) && $options['ssl'] = 0;
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if(is_array($param)){
$param = http_build_query($param);
}
$url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);// 設(shè)置url
!empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 設(shè)置請求頭
if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
}else if(!empty($options['cookie'])){
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解壓gzip頁面內(nèi)容
curl_setopt($ch, CURLOPT_HEADER, 0);// 不獲取請求頭
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 輸出轉(zhuǎn)移,不輸出頁面
!$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服務(wù)器端的驗證ssl
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//偽裝請求來源,繞過防盜
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
//執(zhí)行并獲取內(nèi)容
$output = curl_exec($ch);
//對獲取到的內(nèi)容進(jìn)行操作
if($output === FALSE ){
$result['code'] = 1; // 錯誤
$result['msg'] = "CURL Error:".curl_error($ch);
}
$result['body'] = $output;
//釋放curl句柄
curl_close($ch);
return $result;
}
POST案例
/**
* curl_post
* @param $url 請求地址
* @param null $param get參數(shù)
* @param array $options 配置參數(shù)
* @return array
*/
function curl_post($url,$param = null,$options = array()){
if(empty($options)){
$options = array(
'timeout' => 30,
'header' => array(),
'cookie' => '',
'cookie_file' => '',
'ssl' => 0,
'referer' => null
);
}else{
empty($options['timeout']) && $options['timeout'] = 30;
empty($options['ssl']) && $options['ssl'] = 0;
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if(is_array($param)){
$param = http_build_query($param);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);// 設(shè)置url
!empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 設(shè)置請求頭
if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
}else if(!empty($options['cookie'])){
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解壓gzip頁面內(nèi)容
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_HEADER, 0);// 不獲取請求頭
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 輸出轉(zhuǎn)移,不輸出頁面
!$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服務(wù)器端的驗證ssl
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//偽裝請求來源,繞過防盜
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
//執(zhí)行并獲取內(nèi)容
$output = curl_exec($ch);
//對獲取到的內(nèi)容進(jìn)行操作
if($output === FALSE ){
$result['code'] = 1; // 錯誤
$result['msg'] = "CURL Error:".curl_error($ch);
}
$result['body'] = $output;
//釋放curl句柄
curl_close($ch);
return $result;
}
其他請求類型請自己參考封裝處理
到此這篇關(guān)于php遠(yuǎn)程請求CURL(爬蟲、保存登錄狀態(tài))的文章就介紹到這了,更多相關(guān)php遠(yuǎn)程請求CURL(爬蟲、保存登錄狀態(tài))內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ThinkPHP5實現(xiàn)JWT?Token認(rèn)證的過程(親測可用)
這篇文章主要介紹了ThinkPHP5實現(xiàn)JWT?Token認(rèn)證,首先composer先掛載阿里云鏡像,安裝JWT擴展,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
Linux基于php-fpm模式的lamp搭建phpmyadmin的方法
這篇文章主要介紹了Linux基于php-fpm模式的lamp搭建phpmyadmin的方法,以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。2018-10-10
php 生成自動創(chuàng)建文件夾并上傳文件的示例代碼
本篇文章主要是對php生成自動創(chuàng)建文件夾并上傳文件的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03
利用Laravel生成Gravatar頭像地址的優(yōu)雅方法
Gravatar是一圖像跟隨著您到訪過的網(wǎng)站,當(dāng)您在博客中留言或發(fā)表文章,它將會出現(xiàn)在您的名稱旁。下面這篇文章主要給大家介紹了關(guān)于利用Laravel如何生成 Gravatar 頭像地址的優(yōu)雅方法,需要的朋友可以參考下。2017-12-12
PHP 修復(fù)未正常關(guān)閉的HTML標(biāo)簽實現(xiàn)代碼(支持嵌套和就近閉合)
PHP 修復(fù)未正常關(guān)閉的 HTML 標(biāo)簽 支持嵌套和就近閉合,需要的朋友可以參考下2012-06-06
PHP array_key_exists檢查鍵名或索引是否存在于數(shù)組中的實現(xiàn)方法
下面小編就為大家?guī)硪黄狿HP array_key_exists檢查鍵名或索引是否存在于數(shù)組中的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06

