Я использую следующий код для получения количества твитов из API Twitter:
$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;
Конечно, $ username и другие переменные внутри запроса fopen верны, и он выдает правильный URL, потому что я получаю сообщение об ошибке:
Предупреждение: fopen (http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss&count=5) [function.fopen]: не удалось открыть поток: сбой HTTP-запроса! HTTP / 1.1 400 Неправильный запрос в /home/ellexus1/public_html/settings.php в строке 187
эта ошибка ^^ возвращается всякий раз, когда я пытаюсь открыть свою страницу.
Есть идеи, почему это может быть? Нужно ли мне использовать OAuth, чтобы просто получать мои твиты? Нужно ли регистрировать свой веб-сайт как место, где могут появляться сообщения?
Я действительно не уверен, почему это происходит. Мой хостинг - JustHost.com, но я не уверен, что это будет чем-то другим. Все идеи приветствуются!
Спасибо.
Andrew
PS. Этот код находится внутри функции, в которой правильно передаются имя пользователя, интервал и число, следовательно, в коде ошибки он создал правильно сформированный адрес.