2011-10-20 3 views
0
내가 트위터 API에서 트윗의 양을 검색하려면 다음 코드를 사용하고

말 :트위터 API는 항상 400 잘못된 요청

$cache_file = "cache/$username-twitter.cache"; 
$last = filemtime($cache_file); 
$now = time(); 
$interval = $interval * 60; // ten minutes 

// Check the cache file age 
if (!$last || (($now - $last) > $interval)) { 
    // cache file doesn't exist, or is old, so refresh it 

    // Get the data from Twitter JSON API 
    //$json = @file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" . $username . "&count=" . $count, "rb"); 
    $twitterHandle = fopen("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count", "rb"); 
    $json = stream_get_contents($twitterHandle); 
    fclose($twitterHandle); 

    if($json) { 
     // Decode JSON into array 
     $data = json_decode($json, true); 
     $data = serialize($data); 

     // Store the data in a cache 
     $cacheHandle = fopen($cache_file, 'w'); 
     fwrite($cacheHandle, $data); 
     fclose($cacheHandle); 
    } 

} 

// read from the cache file with either new data or the old cache 
$tweets = @unserialize(file_get_contents($cache_file)); 

return $tweets; 

물론 $ 사용자 이름과 fopen을 요청 내부의 다른 변수 올바른지과

경고 : I 오류 얻을 수 있기 때문에 올바른 URL을 생성하면 fopen을 (http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss & 수 = 5) function.fopen] : 스트림을 열지 못했습니다 : HTTP 요청이 실패했습니다! HTTP/1.1 400 잘못된 요청 /home/ellexus1/public_html/settings.php 온라인 187

내 페이지를 열려고 시도 할 때마다 ^^ 오류가 반환됩니다.

왜 이런 생각일까요? OAuth를 사용하여 내 트윗을 가져와야합니까? 내 웹 사이트를 게시물을 올릴 수있는 곳에 등록해야합니까?

왜 이런 일이 발생하는지 잘 모르겠습니다. 제 호스트는 JustHost.com입니다. 그러나 그것이 어떤 차이가 있는지 확실하지 않습니다. 모든 아이디어는 환영합니다!

감사합니다.

앤드류

ps. 이 코드는 username, interval 및 count가 올바르게 전달되는 함수 내부에 있으므로 오류 코드에서 올바른 형식의 주소가 생성됩니다.

+0

이상한에 제시되어있다

Error codes 

https://dev.twitter.com/overview/api/response-codes

오류 도움이 될 수 fopen

바꾸기, 그것은 나'컬 작동 - D - "http://api.twitter.com/1/statuses/user_timeline.json?screen_n ame = Schodemeiss & count = 5 "' –

답변

6

기회가가 아닌 인증 된 통화 시간당 속도 제한

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.

(150)의 요청을 받고 있습니다 인증 된 통화을 시간당

350 요청 ((IP 주소 지정 기준) 인증 된 사용자 호출을 기반으로)

이러한 오류가 나타나지 않도록 인증해야합니다.

트위터를 다룰 때 cURL도 사용하십시오. file_get_contentsfopen을 사용하여 twitter API를 호출했으며 매우 신뢰할 수 없다는 것을 알았습니다. 당신은 그때마다 그 때마다 타격을 입을 것입니다.

$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$it = curl_exec($ch); //content stored in $it 
curl_close($ch); 
0

이 코드 해상력은 위의 링크

관련 문제