如何在PHP中获取网页的HTML代码? [英] How do I get the HTML code of a web page in PHP?
本文介绍了如何在PHP中获取网页的HTML代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在PHP中检索链接(网页)的HTML代码。例如,如果链接是
https://stackoverflow.com/questions/请问
然后我需要提供的网页的HTML代码。我想检索这个HTML代码并将它存储在一个PHP变量中。
我该怎么做? 解决方案
如果您的PHP服务器允许使用url fopen包装,那么最简单的方法是:
$ html = file_get_contents('http://stackoverflow.com/questions/ask');
如果您需要更多控制,那么您应该查看 cURL 函数:
$ c = curl_init(' http://stackoverflow.com/questions/ask');
curl_setopt($ c,CURLOPT_RETURNTRANSFER,true);
// curl_setopt(...你想要的其他选项...)
$ html = curl_exec($ c);
if(curl_error($ c))
die(curl_error($ c));
//获取状态码
$ status = curl_getinfo($ c,CURLINFO_HTTP_CODE);
curl_close($ c);
I want to retrieve the HTML code of a link (web page) in PHP. For example, if the link is
https://stackoverflow.com/questions/ask
then I want the HTML code of the page which is served. I want to retrieve this HTML code and store it in a PHP variable.
How can I do this?
解决方案
If your PHP server allows url fopen wrappers then the simplest way is:
$html = file_get_contents('http://stackoverflow.com/questions/ask');
If you need more control then you should look at the cURL functions:
$c = curl_init('http://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中获取网页的HTML代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文