【问题标题】:PHP simplexml_load_file error: Too Many Requests?PHP simplexml_load_file 错误:请求太多?
【发布时间】:2013-04-15 04:17:20
【问题描述】:

我正在尝试使用 vimeos API 通过其 XML 文件调用视频的名称。如果我将此代码用于一个 xml 文件,它可以工作文件:

    $location = "http://vimeo.com/api/v2/video/16417063.xml";
    $xml = simplexml_load_file($location);
    echo $xml->video->title;

但是在我将所有 vimeo 视频 ID 存储在数据库中并使用此代码之后:

    <?php
    $seasontwo=mysql_query("SELECT s2 FROM video_ids LIMIT 1");
    while($row=mysql_fetch_array($seasontwo))
    {
    $headline=$row['s2'];
    $location = "http://vimeo.com/api/v2/video/".$headline.".xml";
    $xml = simplexml_load_file($location);
    echo $xml->video->title;
    }
    ?>

我得到错误:

    Warning: simplexml_load_file(http://vimeo.com/api/v2/video/16417063.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests in /home/dpnews0/public_html/tnn/wordpress/wp-content/themes/twentytwelve/content.php on line 11

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://vimeo.com/api/v2/video/16417063.xml" in /home/dpnews0/public_html/tnn/wordpress/wp-content/themes/twentytwelve/content.php on line 11

尽管 xml 文件 http://vimeo.com/api/v2/video/16417063.xml 实际上是有效的。谁能帮我解决这个问题?

【问题讨论】:

  • 我不确定如何解决请求过多的问题
  • 我什至不知道这意味着什么,或者它应该如何影响它

标签: php mysql xml parsing


【解决方案1】:

请求过多是http状态错误码之一,
参考这里:http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error

429 请求过多 (RFC 6585) 用户在给定时间内发送了太多请求。旨在与速率限制方案一起使用。

您收到此错误的原因只是 vimeo 阻止了您的请求,因为您的脚本触发了异常的高活动。这是服务提供商防止内容报废的常见做法(就像您尝试做的那样)。

抛出错误 429 时,您将无法获得有效的 xml 响应。
因此,您将收到 xml I/O 警告的第二条警告消息。

为了解决这个问题,你应该将xml响应(http://vimeo.com/api/v2/video/16417063.xml)存储在本地,以避免重复请求vimeo。

一种简单的方法是将 xml 响应存储到磁盘缓存中。

【讨论】:

    【解决方案2】:

    您正在与另一台不属于您且无法控制的计算机进行交互。因此,其他计算机的行为可能会与您的预期不同。这就是这里发生的情况,您打开 XML 的请求导致错误。如果你不理解错误信息,没问题,你只需要处理它。

    当您与远程服务交互时,您需要设计失败

    $xml = simplexml_load_file($location);
    
    $hasLoaded = (bool) $xml;
    
    if (!$hasLoaded) 
    {
       // handle the case that the remote server could not provide valid XML
       // e.g. provide a stub, search inside a cache or what not:
    
       $xml = new SimpleXMLElement('<r><video><title>Unknown</title></video></r>');
    }
    

    Vimeo 还提供有关它在响应标头中使用的 HTTP 速率限制的元信息 ($http_response_header):

    1. X-RateLimit-Limit: 3600
    2. X-RateLimit-Remaining: 0
    3. X-RateLimit-Reset: 1366033220

    这表明使用的 WAN IP 现在被阻止,直到 2013 年 4 月 15 日星期一 13:40:20 GMT在我触发请求限制(3600 个请求)后一小时。

    我没有得到429 Too Many Requests (RFC6585),而只是403 Forbidden。最后,这都是因为 Vimeo 使用的速率限制。

    变量转储:

    array(17) {
      [ 0] => string(22) "HTTP/1.1 403 Forbidden"
      [ 1] => string(13) "Server: nginx"
      [ 2] => string(35) "Date: Mon, 15 Apr 2013 12:49:05 GMT"
      [ 3] => string(38) "Content-Type: text/html; charset=UTF-8"
      [ 4] => string(17) "Connection: close"
      [ 5] => string(23) "X-RateLimit-Limit: 3600"
      [ 6] => string(24) "X-RateLimit-Remaining: 0"
      [ 7] => string(29) "X-RateLimit-Reset: 1366033220"
      [ 8] => string(38) "Expires: Mon, 15 Apr 2013 13:40:20 GMT"
      [ 9] => string(39) "X-UA-Compatible: IE=EmulateIE9,chrome=1"
      [10] => string(26) "X-DNS-Prefetch-Control: on"
      [11] => string(21) "Vary: Accept-Encoding"
      [12] => string(20) "X-Varnish: 366390796"
      [13] => string(6) "Age: 0"
      [14] => string(16) "Via: 1.1 varnish"
      [15] => string(18) "X-Varnish-Cache: 0"
      [16] => string(24) "X-VServer: 10.90.128.147"
    }
    

    参见:

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 2020-09-24
      • 2017-08-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多