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

My Google Hot Trends PHP code no longer working

我曾经使用此代码来获取 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

有什么解决办法吗?任何帮助表示赞赏。

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