婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > php遠程請求CURL實例教程(爬蟲、保存登錄狀態(tài))

php遠程請求CURL實例教程(爬蟲、保存登錄狀態(tài))

熱門標簽:gps 地圖標注軟件 地圖標注視頻廣告入駐 400電話鄭州申請 黔江400電話如何辦理 ai電話機器人加盟代理 中原區(qū)電話機器人價格 OMG地圖標注app 電銷機器人便宜的有嗎 招標自動語音外呼系統(tǒng)

cURL

cURL可以使用URL的語法模擬瀏覽器來傳輸數據,因為它是模擬瀏覽器,因此它同樣支持多種協(xié)議,F(xiàn)TP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等協(xié)議都可以很好的支持,包括一些:HTTPS認證,HTTP POST方法,HTTP PUT方法,F(xiàn)TP上傳,keyberos認證,HTTP上傳,代理服務器,cookies,用戶名/密碼認證,下載文件斷點續(xù)傳,上傳文件斷點續(xù)傳,http代理服務器管道,甚至它還支持IPv6,scoket5代理服務器,通過http代理服務器上傳文件到FTP服務器等等。

本文主要介紹的是php遠程請求CURL(爬蟲、保存登錄狀態(tài))的相關內容,下面話不多說了,來一起看看詳細的介紹吧

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字符串,瀏覽器直接復制即可
   'cookie_file' => '',// 文件路徑,并要有讀寫權限的
   '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);// 設置url
 !empty($options['header'])  curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 設置請求頭
 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頁面內容
 curl_setopt($ch, CURLOPT_HEADER, 0);// 不獲取請求頭
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 輸出轉移,不輸出頁面
 !$options['ssl']  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服務器端的驗證ssl
 !empty($options['referer'])  curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//偽裝請求來源,繞過防盜
 curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
 //執(zhí)行并獲取內容
 $output = curl_exec($ch);
 //對獲取到的內容進行操作
 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參數
 * @param array $options 配置參數
 * @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);// 設置url
 !empty($options['header'])  curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 設置請求頭
 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頁面內容
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
 curl_setopt($ch, CURLOPT_HEADER, 0);// 不獲取請求頭
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 輸出轉移,不輸出頁面
 !$options['ssl']  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服務器端的驗證ssl
 !empty($options['referer'])  curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//偽裝請求來源,繞過防盜
 curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
 //執(zhí)行并獲取內容
 $output = curl_exec($ch);
 //對獲取到的內容進行操作
 if($output === FALSE ){
  $result['code'] = 1; // 錯誤
  $result['msg'] = "CURL Error:".curl_error($ch);
 }
 $result['body'] = $output;
 //釋放curl句柄
 curl_close($ch);
 return $result;
}

其他請求類型請自己參考封裝處理

到此這篇關于php遠程請求CURL(爬蟲、保存登錄狀態(tài))的文章就介紹到這了,更多相關php遠程請求CURL(爬蟲、保存登錄狀態(tài))內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • php curl發(fā)起get與post網絡請求案例詳解
  • 淺談PHP模擬發(fā)送POST請求之curl基本使用
  • php的curl攜帶header請求頭信息實現(xiàn)http訪問的方法
  • PHP如何使用cURL實現(xiàn)Get和Post請求
  • php curl返回錯誤碼60如何解決

標簽:日照 孝感 北京 阿里 那曲 池州 濟源 哈密

巨人網絡通訊聲明:本文標題《php遠程請求CURL實例教程(爬蟲、保存登錄狀態(tài))》,本文關鍵詞  php,遠程,請求,CURL,實例,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《php遠程請求CURL實例教程(爬蟲、保存登錄狀態(tài))》相關的同類信息!
  • 本頁收集關于php遠程請求CURL實例教程(爬蟲、保存登錄狀態(tài))的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 盐城市| 宜城市| 大同市| 和林格尔县| 都昌县| 茶陵县| 开鲁县| 永胜县| 龙泉市| 邢台市| 阿坝| 嘉义县| 汶川县| 阳江市| 大竹县| 岑溪市| 南充市| 合作市| 沧州市| 双柏县| 普格县| 特克斯县| 兴城市| 张北县| 钟山县| 平安县| 信宜市| 阿拉尔市| 康定县| 许昌县| 宣威市| 晴隆县| 壤塘县| 天津市| 安吉县| 安顺市| 天气| 牙克石市| 股票| 嘉黎县| 霍林郭勒市|