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

首頁 > 網站 > 建站經驗 > 正文

PHP中_4種常用的抓取網絡數據方法

2019-11-02 14:16:06
字體:
來源:轉載
供稿:網友

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

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

  ?

1 2 3 $url = 'http://localhost/test2.php'; $html = file_get_conte
qq搞笑網名[www.la240.com/html2017/1/5/]
nts($url); echo $html;

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

  ?

1 2 3 4 5 6 7 8 9 10 $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函數,以post方式獲取url

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 $data = array( 'foo'=>'bar', 'baz'=>'boom', 'site'=>'www.jb51.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擴展

  ?

1 2 3 4 5 6 7 8 9 $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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井研县| 舒兰市| 罗源县| 桐乡市| 天长市| 河间市| 榕江县| 万载县| 独山县| 七台河市| 贵德县| 五常市| 彰武县| 安溪县| 盱眙县| 崇明县| 比如县| 舟曲县| 淄博市| 礼泉县| 松滋市| 邯郸市| 河南省| 浙江省| 湖北省| 庆阳市| 托克托县| 台北市| 宽甸| 高平市| 浦北县| 武陟县| 获嘉县| 四川省| 桃江县| 武安市| 扬中市| 沛县| 正定县| 江山市| 台前县|