最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP中4種常用的抓取網絡數(shù)據(jù)方法

 更新時間:2015年06月04日 10:58:46   投稿:junjie  
這篇文章主要介紹了PHP中4種常用的抓取網絡數(shù)據(jù)方法,本文講解使用file_get_contents函數(shù)、fopen函數(shù)、curl庫三種常見方法抓取網絡數(shù)據(jù),并給出了代碼實例,需要的朋友可以參考下

本小節(jié)的名稱為 fsockopen,curl與file_get_contents,具體是探討這三種方式進行網絡數(shù)據(jù)輸入輸出的一些匯總。關于 fsockopen 前面已經談了不少,下面開始轉入其它。這里先簡單羅列一下一些常見的抓取網絡數(shù)據(jù)的一些方法。

1. 用 file_get_contents 以 get 方式獲取內容:

$url = 'http://localhost/test2.php';
$html = file_get_contents($url);
echo $html;

2. 用fopen打開url,以get方式獲取內容

$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'=>'m.fzitv.net',
 '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是否已經打開了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;

相關文章

最新評論

安丘市| 桐城市| 甘德县| 南昌县| 甘洛县| 奉贤区| 利辛县| 绥宁县| 台北县| 恭城| 阳泉市| 阳新县| 厦门市| 民权县| 塔城市| 田阳县| 肥乡县| 壤塘县| 新沂市| 响水县| 绥滨县| 昌都县| 启东市| 元阳县| 封丘县| 友谊县| 乳山市| 易门县| 和平区| 赫章县| 阿鲁科尔沁旗| 廉江市| 和硕县| 武平县| 文成县| 宝应县| 巴彦县| 乐业县| 黎城县| 涡阳县| 旌德县|