PHP中4種常用的抓取網(wǎng)絡(luò)數(shù)據(jù)方法
本小節(jié)的名稱為 fsockopen,curl與file_get_contents,具體是探討這三種方式進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)輸入輸出的一些匯總。關(guān)于 fsockopen 前面已經(jīng)談了不少,下面開始轉(zhuǎn)入其它。這里先簡單羅列一下一些常見的抓取網(wǎng)絡(luò)數(shù)據(jù)的一些方法。
1. 用 file_get_contents 以 get 方式獲取內(nèi)容:
$url = 'http://localhost/test2.php'; $html = file_get_contents($url); echo $html;
2. 用fopen打開url,以get方式獲取內(nèi)容
$url = 'http://localhost/test2.php'; $fp = fopen($url, 'r'); stream_get_meta_data($fp); $result = ''; while(!feof($fp)) { $result .= fgets($fp, 1024); } echo "url body: $result"; fclose($fp);
3. 用file_get_contents函數(shù),以post方式獲取url
$data = array( 'foo'=>'bar', 'baz'=>'boom', 'site'=>'www.dbjr.com.cn', 'name'=>'nowa magic'); $data = http_build_query($data); //$postdata = http_build_query($data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $data //'timeout' => 60 * 60 // 超時(shí)時(shí)間(單位:s) ) ); $url = "http://localhost/test2.php"; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;
4、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經(jīng)打開了curl擴(kuò)展
$url = 'http://localhost/test2.php?site=jb51.net'; $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents;
- PHP+fiddler抓包采集微信文章閱讀數(shù)點(diǎn)贊數(shù)的思路詳解
- Fiddler如何抓取手機(jī)APP數(shù)據(jù)包
- fiddler抓包小技巧之自動(dòng)保存抓包數(shù)據(jù)的實(shí)現(xiàn)方法分析【可根據(jù)需求過濾】
- Python如何爬取微信公眾號(hào)文章和評(píng)論(基于 Fiddler 抓包分析)
- 抓包工具Fiddler的使用方法詳解(Fiddler中文教程)
- Fiddler實(shí)現(xiàn)手機(jī)抓包之小白入門必看
- 利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法
- PHP的cURL庫功能簡介 抓取網(wǎng)頁、POST數(shù)據(jù)及其他
- php使用curl代理實(shí)現(xiàn)抓取數(shù)據(jù)的方法
- PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù))
- PHP配合fiddler抓包抓取微信指數(shù)小程序數(shù)據(jù)的實(shí)現(xiàn)方法分析
相關(guān)文章
php調(diào)整gif動(dòng)畫圖片尺寸示例代碼分享
這篇文章主要介紹了如何使用php調(diào)整gif動(dòng)畫圖片尺寸,大家可以參考使用2013-12-12PHP中通過ADO調(diào)用Access數(shù)據(jù)庫的方法測試不通過
PHP中通過ADO調(diào)用Access數(shù)據(jù)庫的方法測試不通過...2006-12-12PHP數(shù)據(jù)庫調(diào)用類調(diào)用實(shí)例(詳細(xì)注釋)
PHP開發(fā)中我們經(jīng)常需要用一些數(shù)據(jù)庫類,這里簡單的分享下調(diào)用類的代碼,學(xué)習(xí)php數(shù)據(jù)庫操作的朋友可以參考下2012-07-07php獲取一定范圍內(nèi)取N個(gè)不重復(fù)的隨機(jī)數(shù)
這篇文章主要介紹了php獲取一定范圍內(nèi)取N個(gè)不重復(fù)的隨機(jī)數(shù)的方法,通過range函數(shù)創(chuàng)建指定范圍內(nèi)數(shù)組及shuffle進(jìn)行數(shù)組隨機(jī)排序,并使用array_slice抽取數(shù)組實(shí)現(xiàn)該功能,非常簡單實(shí)用,需要的朋友可以參考下2016-05-05