首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >file_get_contents脚本适用于某些网站,但不适用于其他网站

file_get_contents脚本适用于某些网站,但不适用于其他网站
EN

Stack Overflow用户
提问于 2011-08-02 02:53:54
回答 3查看 10.3K关注 0票数 4

我希望建立一个PHP脚本,为特定的标签解析HTML。我一直在使用这个代码块,改编自这个tutorial

代码语言:javascript
复制
<?php 
$data = file_get_contents('http://www.google.com');
$regex = '/<title>(.+?)</';
preg_match($regex,$data,$match);
var_dump($match); 
echo $match[1];
?>

该脚本适用于一些网站(如google,上图),但当我在其他网站(如freshdirect)上尝试它时,我收到以下错误:

“警告: file_get_contents(http://www.freshdirect.com)函数.file-get-contents:无法打开流: HTTP请求失败!”

我在StackOverflow上见过很多很棒的suggestions,比如在php.ini中启用extension=php_openssl.dll。但是(1)我的php.ini版本中没有extension=php_openssl.dll,(2)当我将它添加到扩展部分并重新启动WAMP服务器时,根据这个thread,仍然没有成功。

有没有人介意给我指个方向?非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-02 02:58:49

代码语言:javascript
复制
$html = file_get_html('http://google.com/');
$title = $html->find('title')->innertext;

或者如果你更喜欢使用preg_match,并且你真的应该使用cURL而不是fgc...

代码语言:javascript
复制
function curl($url){

    $headers[]  = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
    $headers[]  = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    $headers[]  = "Accept-Language:en-us,en;q=0.5";
    $headers[]  = "Accept-Encoding:gzip,deflate";
    $headers[]  = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $headers[]  = "Keep-Alive:115";
    $headers[]  = "Connection:keep-alive";
    $headers[]  = "Cache-Control:max-age=0";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_ENCODING, "gzip");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;

}


$data = curl('http://www.google.com');
$regex = '#<title>(.*?)</title>#mis';
preg_match($regex,$data,$match);
var_dump($match); 
echo $match[1];
票数 3
EN

Stack Overflow用户

发布于 2011-08-02 03:27:06

它只需要一个用户代理(实际上是“any”,任何字符串就足够了):

代码语言:javascript
复制
file_get_contents("http://www.freshdirect.com",false,stream_context_create(
    array("http" => array("user_agent" => "any"))
));

参见more options

当然,您可以在您的ini中设置user_agent

代码语言:javascript
复制
 ini_set("user_agent","any");
 echo file_get_contents("http://www.freshdirect.com");

..。但我更愿意对下一位从事这项工作的程序员说得更清楚。

票数 5
EN

Stack Overflow用户

发布于 2011-08-02 03:31:16

另一种选择:一些主机禁用了CURLOPT_FOLLOWLOCATION,所以递归是你想要的,也会将任何错误记录到文本文件中。还有一个如何使用DOMDocument()提取内容的简单示例,显然它不是很广泛,但您可以构建应用程序。

代码语言:javascript
复制
<?php 
function file_get_site($url){
(function_exists('curl_init')) ? '' : die('cURL Must be installed. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
$curl = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);

$html = curl_exec($curl);

$status = curl_getinfo($curl);
curl_close($curl);

if($status['http_code']!=200){
    if($status['http_code'] == 301 || $status['http_code'] == 302) {
        list($header) = explode("\r\n\r\n", $html, 2);
        $matches = array();
        preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
        $url = trim(str_replace($matches[1],"",$matches[0]));
        $url_parsed = parse_url($url);
        return (isset($url_parsed))? file_get_site($url):'';
    }
    $oline='';
    foreach($status as $key=>$eline){$oline.='['.$key.']'.$eline.' ';}
    $line =$oline." \r\n ".$url."\r\n-----------------\r\n";
    $handle = @fopen('./curl.error.log', 'a');
    fwrite($handle, $line);
    return FALSE;
}
return $html;
}


function get_content_tags($source,$tag,$id=null,$value=null){
    $xml = new DOMDocument();
    @$xml->loadHTML($source);

    foreach($xml->getElementsByTagName($tag) as $tags) {
        if($id!=null){
            if($tags->getAttribute($id)==$value){
                return $tags->getAttribute('content');
            }
        }
        return $tags->nodeValue;
    }
}


$source = file_get_site('http://www.freshdirect.com/about/index.jsp');

echo get_content_tags($source,'title'); //FreshDirect

echo get_content_tags($source,'meta','name','description'); //Online grocer providing high quality fresh......

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6902955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档