溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php執(zhí)行抓取網(wǎng)頁(yè)的幾種方式

發(fā)布時(shí)間:2020-08-17 14:09:11 來源:網(wǎng)絡(luò) 閱讀:673 作者:Lee_吉 欄目:web開發(fā)

一、遠(yuǎn)程php代碼:

<?php
    header('access-allow-origin:*');
    sleep(1);
    echo "hello\n";
    echo "world";

二、具體實(shí)現(xiàn):

  1. file函數(shù):
    a. 代碼:
    <?php
    $url = 'http://localhost/test.php';
    $output = file($url);
    var_dump($output);

    b. 輸出:

    array(2) {
    [0]=>
    string(6) "hello"
    [1]=>
    string(5) "world"
    }
  2. file_get_contents函數(shù):
    a. 代碼:
    <?php
    $url = 'http://localhost/test.php';
    $output = file_get_contents($url);
    var_dump($output);

    b. 輸出:

    string(11) "hello world"
  3. fopen函數(shù):
    a. 代碼:
    <?php
    $url = 'http://localhost/test.php'; 
    $handle = fopen($url,"rb");
    do{     
            $data = fread($handle,1024);     
            if(strlen($data)==0) {
                    break;   
            }     
            $output = $data;
    }  
    while(true);
    fclose($handle);
    var_dump( $output);

    b. 輸出:

    string(11) "hello world"
  4. curl函數(shù):
    a. 代碼:
    <?php
    $url = 'http://localhost/test.php'; 
    $ch = curl_init();
    $timeout = 1;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);

    b. 輸出:

    string(11) "hello world"
  5. fsockopen函數(shù):
    a. 代碼:
    <?php
    $url = 'localhost/test.php'; 
    $fp = fsockopen($url, 80, $errno, $errstr, 30);
    if (!$fp) {
            echo "$errstr ($errno)<br />\n";
    } else {
        stream_set_blocking($fp,0);
            $out = "GET / HTTP/1.1\r\n";
            $out .= "Host: {$url}\r\n";
            $out .= "Connection: Close\r\n\r\n";
            fwrite($fp, $out);
            while (!feof($fp)) {
                    var_dump(fgets($fp, 128));
            }
            fclose($fp);
    }

    b. 輸出:

    string(11) "hello world"
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI