我想在PHP中检索链接(网页)的HTML代码。例如,如果链接是
那么我要提供该页面的HTML代码。我想检索此HTML代码并将其存储在PHP变量中。
我怎样才能做到这一点?
我想在PHP中检索链接(网页)的HTML代码。例如,如果链接是
那么我要提供该页面的HTML代码。我想检索此HTML代码并将其存储在PHP变量中。
我怎样才能做到这一点?
Answers:
如果您的PHP服务器允许使用url fopen包装器,则最简单的方法是:
$html = file_get_contents('/programming/ask');
如果需要更多控制,则应查看cURL函数:
$c = curl_init('/programming/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 DOM解析器。我发现PHP简单HTML DOM解析器 非常易于使用。
您可能想从Yahoo签出YQL库:http : //developer.yahoo.com/yql
手头的任务很简单
select * from html where url = 'http://stackoverflow.com/questions/ask'
您可以在控制台中尝试以下操作:http : //developer.yahoo.com/yql/console(需要登录)
另请参阅Chris Heilmanns截屏,以获取一些不错的想法,您还可以做什么:http : //developer.yahoo.net/blogs/theater/archives/2009/04/screencast_collating_distributed_information.html
简单方法:使用file_get_contents():
$page = file_get_contents('http://stackoverflow.com/questions/ask');
请注意,allow_url_fopen一定要true在你php.ini能够使用URL的fopen封装。
更高级的方法:如果您无法更改PHP配置(默认情况下allow_url_fopen是)false,并且已安装ext / curl,请使用该cURL库连接到所需的页面。
include_once('simple_html_dom.php');
$url="http://stackoverflow.com/questions/ask";
$html = file_get_html($url);
您可以使用此代码以数组(解析形式)的形式获取整个HTML代码,在此处http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download下载'simple_html_dom.php'文件
这是两种从URL获取内容的简单方法:
1)第一种方法
从您的主机(php.ini或其他地方)启用Allow_url_include
<?php
$variableee = readfile("http://example.com/");
echo $variableee;
?>
要么
2)第二种方法
启用php_curl,php_imap和php_openssl
<?php
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_dataa($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$variableee = get_dataa('http://example.com');
echo $variableee;
?>
您也可以使用DomDocument方法来获取单个HTML标签级别的变量
$homepage = file_get_contents('https://www.example.com/');
$doc = new DOMDocument;
$doc->loadHTML($homepage);
$titles = $doc->getElementsByTagName('h3');
echo $titles->item(0)->nodeValue;