php执行抓取网页的几种方式
<?php header('access-allow-origin:*'); sleep(1); echo "hello\n"; echo "world";
二、具体实现: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"}
file_get_contents函数:
a. 代码:
<?php$url = 'http://localhost/test.php';$output = file_get_contents($url);var_dump($output);
b. 输出:
string(11) "hello world"
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"
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"
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"
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。