我的Google热门趋势PHP代码不再有效

时间:2017-02-22 11:04:39

标签: php google-trends

我曾经使用此代码获取Google热门趋势列表,但它最近停止了工作,并且它已停止在我获得此代码的网站上工作。这是代码的基本示例:

   <?php

    $url = 'http://www.google.com/trends/hottrends/atom/feed?pn=p1';
    $referrer = 'http://www.google.com';
    $agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    $result = curl_exec($ch);

     $trends = new SimpleXmlElement($result);

     foreach($trends->channel->item as $value) { 
            echo $value->title."<br>";
     }

    ?>

这些是错误:

Warning: SimpleXMLElement::__construct(): Entity: line 2: parser error : Opening and ending tag mismatch: meta line 1 and HEAD in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): <TITLE>301 Moved</TITLE></HEAD><BODY> in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): ^ in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: HEAD line 1 and HTML in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): </BODY></HTML> in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): ^ in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HTML line 1 in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Warning: SimpleXMLElement::__construct(): ^ in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php:34 Stack trace: #0 /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php(34): SimpleXMLElement->__construct('<HTML><HEAD><me...') #1 {main} thrown in /home/######/public_html/examples/get_latest_google_trends_with_php/example1.php on line 34

任何想法如何解决这个问题?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

该服务的网址已更改。只需将$ url值替换为:https://trends.google.com/trends/hottrends/atom/feed?pn=p1

相关问题