gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:42:42 24 4
gpt4 key购买 nike

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

最佳答案

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

1) 使用 file_get_contents()

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

如果此方法失败(由于 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>';
?>

关于php - 如何通过php获取不在我的服务器中的任何网页的所有内容(HTML代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3583837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com