PHP中4種常用的抓取網(wǎng)絡數(shù)據(jù)方法
本小節(jié)的名稱為 fsockopen,curl與file_get_contents,具體是探討這三種方式進行網(wǎng)絡數(shù)據(jù)輸入輸出的一些匯總。關于 fsockopen 前面已經(jīng)談了不少,下面開始轉入其它。這里先簡單羅列一下一些常見的抓取網(wǎng)絡數(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.dhdzp.com', '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 // 超時時間(單位: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擴展
$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ù)點贊數(shù)的思路詳解
- Fiddler如何抓取手機APP數(shù)據(jù)包
- fiddler抓包小技巧之自動保存抓包數(shù)據(jù)的實現(xiàn)方法分析【可根據(jù)需求過濾】
- Python如何爬取微信公眾號文章和評論(基于 Fiddler 抓包分析)
- 抓包工具Fiddler的使用方法詳解(Fiddler中文教程)
- Fiddler實現(xiàn)手機抓包之小白入門必看
- 利用Fiddler對手機進行抓包的實現(xiàn)方法
- PHP的cURL庫功能簡介 抓取網(wǎng)頁、POST數(shù)據(jù)及其他
- php使用curl代理實現(xiàn)抓取數(shù)據(jù)的方法
- PHP的CURL方法curl_setopt()函數(shù)案例介紹(抓取網(wǎng)頁,POST數(shù)據(jù))
- PHP配合fiddler抓包抓取微信指數(shù)小程序數(shù)據(jù)的實現(xiàn)方法分析
相關文章
php中使用in_array() foreach array_search() 查找數(shù)組是否包含時的性能對比
這篇文章主要介紹了php中使用in_array() foreach array_search() 查找數(shù)組是否包含時的性能對比,需要的朋友可以參考下2015-04-04
PHP中file_exists函數(shù)不支持中文名的解決方法
這篇文章主要介紹了PHP中file_exists函數(shù)不支持中文名的解決方法,很實用的技巧,需要的朋友可以參考下2014-07-07
PHP基于關聯(lián)數(shù)組20行代碼搞定約瑟夫問題示例
這篇文章主要介紹了PHP基于關聯(lián)數(shù)組20行代碼搞定約瑟夫問題,結合具體實例分析了php使用關聯(lián)數(shù)組解決約瑟夫問題的相關操作技巧,需要的朋友可以參考下2017-11-11

