PHP simplexml_load_file错误:请求太多?

时间:2013-04-15 04:17:20

标签: php mysql xml parsing

我正在尝试使用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实际上是有效的。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

请求太多是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响应存储到磁盘缓存中。

答案 1 :(得分:0)

您正在与另一台您不拥有的计算机进行交互,而您无法控制。因此,其他计算机可以根据您的预期改变其行为。这就是这里发生的事情,您打开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
  4. 这表明使用的WAN IP现在被阻止,直到 Mon,2013年4月15日13:40:20 GMT 。在我触发请求限制(3600个请求)后一小时。

    我没有得到429 Too Many Requests (RFC6585),只有 403 Forbidden 。最后,由于Vimeo使用速率限制,它们都是。

    VAR-转储:

    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"
    }
    

    参见: