我想在 PHP 中检索链接(网页)的 HTML 代码。例如,如果链接是
https://stackoverflow.com/questions/ask
然后我想要提供的页面的 HTML 代码。我想检索此 HTML 代码并将其存储在 PHP 变量中。
我该怎么做?
最佳答案
如果您的 PHP 服务器允许 url fopen 包装器,那么最简单的方法是:
$html = file_get_contents('https://stackoverflow.com/questions/ask');
如果您需要更多控制,那么您应该查看 cURL功能:
$c = curl_init('https://stackoverflow.com/questions/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
关于php - 如何在 PHP 中获取网页的 HTML 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/819182/