如何通过php获取不在我的服务器中的任何网页的所有内容(HTML代码)

far*_*oft 1 html php curl get

如何通过php获取不在我的服务器中的任何网页的所有内容(HTML代码)

sha*_*mar 7

打印google.com主页内容(HTML)的两种简单方法:

1)使用 file_get_contents()

<?php
$content = file_get_contents("http://www.google.com/");
echo '<pre>'.htmlspecialchars($content).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)

如果此方法失败(由于未启用URL fopen包装器,请使用下面的第二种方法).

2)使用cURL:

<?php
function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$content = file_get_contents_curl("http://www.google.com/");
echo '<pre>'.htmlspecialchars($content).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)