<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            php抓取頁面的的方法

            時間:2025-08-10 10:02:19 php語言

            php抓取頁面的的方法

              導語:抓取和分析一個文件是非常簡單的事。下面是php抓取頁面的的方法,希望對你有所幫助:

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

              1. file()函數

              2. file_get_contents()函數

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

              4.curl方式

              5. fsockopen()函數 socket模式

              6. 使用插件

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

              1. file()函數

              $url='/pic/p>

              $lines_array=file($url);

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

              echo htmlspecialchars($lines_string);

              2. file_get_contents()函數

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

              $url='/pic/p>

              $lines_string=file_get_contents($url);

              echo htmlspecialchars($lines_string);

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

              $url='/pic/p>

              $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擴展。

              $url='/pic/p>

              $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()函數 socket模式

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

              $fp = fsockopen("udp:/pic/p>

              if (!$fp) {

              echo "ERROR: $errno - $errstr
            "

              } else {

              fwrite($fp, " ")

              echo fread($fp, 26)

              fclose($fp)

              }


            【php抓取頁面的的方法】相關文章:

            php抓取頁面的方法03-10

            php抓取頁面的幾種方法09-27

            PHP列表頁實現的方法03-16

            php靜態頁生成方法03-11

            php抓取https的內容的代碼08-09

            PHP實現抓取HTTPS內容02-11

            php技術生成靜態頁面的方法12-25

            PHP如何使用curl實現數據抓取02-05

            自學PHP方法08-15

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看