温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php执行抓取网页的几种方式

发布时间:2020-08-17 14:09:11 来源:网络 阅读:783 作者:Lee_吉 栏目:web开发

一、远程php代码:

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

二、具体实现:

  1. file函数:
    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函数:
    a. 代码:
    <?php $url = 'http://localhost/test.php'; $output = file_get_contents($url); var_dump($output);

    b. 输出:

    string(11) "hello world"
  3. fopen函数:
    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函数:
    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函数:
    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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI