色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:92日期:2022-09-08 14:43:22

cURL

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

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

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協議 ’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’]); //執行并獲取內容 $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’]); //執行并獲取內容 $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(爬蟲、保存登錄狀態)的文章就介紹到這了,更多相關php遠程請求CURL(爬蟲、保存登錄狀態)內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: PHP
相關文章:
主站蜘蛛池模板: 有码 在线 | 欧美日本一道道一区二区三 | 色偷偷资源网 | 亚洲国产一区二区a毛片 | 日韩色网站 | 一区二区三区在线播放 | 成人看片黄a免费 | 亚洲 欧美 精品 中文第三 | 9191精品国产免费不久久 | 一级毛片日韩 | 欧美高清一级毛片免费视 | 全部免费毛片免费播放 | 免费看成人www的网站软件 | 欧美一级高清黄图片 | 国产成在线观看免费视频 | 成人欧美网站 | 午夜视频一区二区 | 国产精品亚洲片在线观看不卡 | 在线亚洲精品 | 国产思思 | 中文在线最新版天堂 | 男女男精品视频网站在线观看 | 在线免费国产 | 欧美xxxxx色视频在线观看 | 国内免费视频成人精品 | 日日a.v拍夜夜添久久免费 | 久久国产夜色精品噜噜亚洲a | 久久精品一区二区免费看 | 中文字幕亚洲不卡在线亚瑟 | 德国女人一级毛片免费 | 国产成a人亚洲精v品久久网 | 日本 欧美 在线 | 久久一级视频 | 成 人色 网 站 欧美大片在线观看 | 99精品在线播放 | 高清精品女厕在线观看 | 中文在线视频观看 | 亚洲性久久久影院 | 中文字幕在线成人免费看 | 国语精品视频在线观看不卡 | 国产麻豆入在线观看 |