代码之家  ›  专栏  ›  技术社区  ›  Joel Glovier

为什么这个Twitter API脚本会失败?

  •  1
  • Joel Glovier  · 技术社区  · 16 年前

    我从这个Twitter脚本中得到一个错误,导致页面的其余部分无法加载。不知道为什么突然发生这种情况,在那里它已经正常运行了很长一段时间。

    <?php  
    $response = new SimpleXMLElement('http://twitter.com/users/show/tuscaroratackle.xml',NULL,TRUE);  
    echo $response->status->text.'';  
    ?>
    

    这是另一篇文章,我试图找出另一个错误的答案,它指出了这个Twitter错误。

    here 在页脚或输出的屏幕抓图中: http://cl.ly/33IZ .

    1 回复  |  直到 16 年前
        1
  •  2
  •   Lee    16 年前

    相关错误(显示在链接到的页的页脚中)是:

    Warning: SimpleXMLElement::__construct(http://twitter.com/users/show/tuscaroratackle.xml) [simplexmlelement.--construct]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home5/tuscaror/public_html/footer.php on line 47
    
    Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "http://twitter.com/users/show/tuscaroratackle.xml" in /home5/tuscaror/public_html/footer.php on line 47
    
    Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home5/tuscaror/public_html/footer.php:47 Stack trace: #0 /home5/tuscaror/public_html/footer.php(47): SimpleXMLElement->__construct('http://twitter....', 0, true) #1 /home5/tuscaror/public_html/index.php(119): include('/home5/tuscaror...') #2 {main} thrown in /home5/tuscaror/public_html/footer.php on line 47
    

    因此,由于某种原因,当向twitter发出HTTP请求以检索文档时,服务器将失败 "http://twitter.com/users/show/tuscaroratackle.xml" . 返回码是 400 Bad Request .


    这是什么 twitter has to say about it :

    400 Bad Request: The request was invalid.  An accompanying error message 
    will explain why. This is the status code will be returned during rate limiting.
    

    Here is twitter's page on Rate Limiting . 我怀疑这是你的罪魁祸首。如果您不这么认为,那么您可以尝试将文档作为字符串检索并检查它 之前

    这是一个快速和肮脏的,但它会得到信息,这样你就可以看到发生了什么:

    $str = file_get_contents('http://twitter.com/users/show/tuscaroratackle.xml');
    echo $str;
    

    可能由于400响应代码而失败。如果是,你需要使用 php curl 以获取未解析的响应体。

    祝你好运!