2012-03-12 10 views
1

とTwitterのAPIを使用しているとき、私はリストにHTTPリクエストは、simplexml_load_file

if(($xml = simplexml_load_file('http://api.twitter.com/1/statuses/user_timeline.xml?count=5&screen_name=les_sismo')) !== FALSE) { 
    $tweets = $xml->xpath("/statuses/status"); 

    foreach($tweets as $tweet) { 
     $text = $tweet->text; 
     echo '<li>' . $text . '</li>'; 
    } 
} 
else echo 'error'; 

そして、私が得たすべての2警告

Warning: simplexml_load_file(http://api.twitter.com/1/statuses/user_timeline.xml?count=5&screen_name=les_sismo) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /homez.466/sismodes/www/wp-content/themes/sismo/header.php on line 89 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://api.twitter.com/1/statuses/user_timeline.xml?count=5&screen_name=les_sismo" in /homez.466/sismodes/www/wp-content/themes/sismo/header.php on line 89 
error 

答えて

3

ツイッターを持っているのTwitterアカウントの5最後のつぶやきを表示したい失敗しましたAPI呼び出しの厳格な制限があります。上限を超えた場合は、400のBad Requestエラーが発生します。詳細については、Rate Limiting FAQ

これを回避するには、HTTPヘッダーで肯定的な応答を確認することができます。

$url = "http://twitter.com/statuses/user_timeline/les_sismo.xml?count=5"; 
$url_headers = @get_headers($url); 
if($url_headers[0] == 'HTTP/1.1 200 OK') { 
    $xml = simplexml_load_file($url); 
} else { 
    // Error 
    exit("failed to load XML"); 
} 

XMLが不十分な可能なAPI呼び出しにアクセスできない期間を回避するために、あなたはおそらくローカルxmlファイルをキャッシュし、上記ELSE文にフォールバックとしてその上で呼び出すことができます。