av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

php抓取頁面的方法

時間:2025-03-10 19:22:25 php語言 我要投稿
  • 相關(guān)推薦

php抓取頁面的方法

  使用php抓取指定頁面部分并顯示的方法有哪些呢?下面小編給大家整理了php抓取頁面的方法,一起來學(xué)習(xí)吧!

  一、 PHP抓取頁面的主要方法:

  1. file()函數(shù)

  2. file_get_contents()函數(shù)

  3. fopen()->fread()->fclose()模式

  4.curl方式

  5. fsockopen()函數(shù) socket模式

  6. 使用插件(如:http://sourceforge.net/projects/snoopy/)

  二、PHP解析html或xml代碼主要方式:

  1. file()函數(shù)

  <?php

  $url='http://t.qq.com';

  $lines_array=file($url);

  $lines_string=implode('',$lines_array);

  echo htmlspecialchars($lines_string);

  2. file_get_contents()函數(shù)

  使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設(shè)置 allow_url_fopen = On,allow_url_fopen關(guān)閉時fopen和file_get_contents都不能打開遠(yuǎn)程文件。

  <?php

  $url='http://t.qq.com';

  $lines_string=file_get_contents($url);

  echo htmlspecialchars($lines_string);

  3. fopen()->fread()->fclose()模式

  <?php

  $url='http://t.qq.com';

  $handle=fopen($url,"rb");

  $lines_string="";

  do{

  $data=fread($handle,1024);

  if(strlen($data)==0) {

  break;

  }

  $lines_string.=$data;

  }while(true);

  fclose($handle);

  echo htmlspecialchars($lines_string);

  4. curl方式

  使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而且需 要拷貝ssleay32.dll和libeay32.dll到C:WINDOWSsystem32下;Linux下要安裝curl擴(kuò)展。

  <?php

  $url='http://t.qq.com';

  $ch=curl_init();

  $timeout=5;

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

  $lines_string=curl_exec($ch);

  curl_close($ch);

  echo htmlspecialchars($lines_string);

  5. fsockopen()函數(shù) socket模式

  socket模式能否正確執(zhí)行,也跟服務(wù)器的設(shè)置有關(guān)系,具體可以通過phpinfo查看服務(wù)器開啟了哪些通信協(xié)議,比如我的本地php socket沒開啟http,只能使用udp測試一下了。

  <?php

  $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);

  if (!$fp) {

  echo "ERROR: $errno - $errstr<br /> "

  } else {

  fwrite($fp, " ")

  echo fread($fp, 26)

  fclose($fp)

  }  


【php抓取頁面的方法】相關(guān)文章:

php抓取頁面的的方法06-09

php抓取頁面的幾種方法10-01

PHP列表頁實現(xiàn)的方法05-24

php靜態(tài)頁生成方法10-25

PHP實現(xiàn)抓取HTTPS內(nèi)容09-17

php抓取https的內(nèi)容的代碼08-18

php技術(shù)生成靜態(tài)頁面的方法07-28

PHP如何使用curl實現(xiàn)數(shù)據(jù)抓取09-27

自學(xué)PHP方法09-24