国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

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

2024-05-04 23:35:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了PHP中4種常用的抓取網(wǎng)絡(luò)數(shù)據(jù)方法,本文講解使用file_get_contents函數(shù)、fopen函數(shù)、curl庫(kù)三種常見(jiàn)方法抓取網(wǎng)絡(luò)數(shù)據(jù),并給出了代碼實(shí)例,需要的朋友可以參考下

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

1. 用 file_get_contents 以 get 方式獲取內(nèi)容:

 

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

2. 用fopen打開(kāi)url,以get方式獲取內(nèi)容

 

 
  1. $url = 'http://localhost/test2.php'
  2. $fp = fopen($url'r'); 
  3. stream_get_meta_data($fp); 
  4. $result = ''
  5. while(!feof($fp)) 
  6. $result .= fgets($fp, 1024); 
  7. echo "url body: $result"
  8. fclose($fp); 

3. 用file_get_contents函數(shù),以post方式獲取url

 

 
  1. $data = array
  2. 'foo'=>'bar'
  3. 'baz'=>'boom'
  4. 'site'=>'m.survivalescaperooms.com'
  5. 'name'=>'nowa magic'); 
  6.  
  7. $data = http_build_query($data); 
  8.  
  9. //$postdata = http_build_query($data); 
  10. $options = array
  11. 'http' => array
  12. 'method' => 'POST'
  13. 'header' => 'Content-type:application/x-www-form-urlencoded'
  14. 'content' => $data 
  15. //'timeout' => 60 * 60 // 超時(shí)時(shí)間(單位:s) 
  16. ); 
  17.  
  18. $url = "http://localhost/test2.php"
  19. $context = stream_context_create($options); 
  20. $result = file_get_contents($url, false, $context); 
  21.  
  22. echo $result

4、使用curl庫(kù),使用curl庫(kù)之前,可能需要查看一下php.ini是否已經(jīng)打開(kāi)了curl擴(kuò)展

 

 
  1. $url = 'http://localhost/test2.php?site=vevb.com'
  2. $ch = curl_init(); 
  3. $timeout = 5; 
  4. curl_setopt ($ch, CURLOPT_URL, $url); 
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  7. $file_contents = curl_exec($ch); 
  8. curl_close($ch); 
  9. echo $file_contents

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白河县| 吴川市| 凤冈县| 福贡县| 岢岚县| 合阳县| 伽师县| 彭泽县| 延庆县| 西吉县| 诸暨市| 土默特右旗| 银川市| 镇巴县| 红河县| 河南省| 舞钢市| 新平| 南郑县| 吉林市| 双柏县| 阿克| 苏尼特右旗| 铜山县| 策勒县| 南皮县| 禹城市| 云林县| 淮北市| 德惠市| 长阳| 天全县| 会同县| 南昌县| 辽宁省| 鄂州市| 加查县| 阿克陶县| 齐河县| 安义县| 肇州县|