Webページの簡単なTwitterフィードを作成しました。API の JSON 応答を含むキャッシュ ファイルを保存し、jQuery を使用して読み取ります。
問題は、REST API の制限である 150 リクエストにランダムに達することですが、私は 1 時間に 6 件 (10 分ごとに 1 件) しか処理していません。覚えている限り、他のフィードはありません。私のホスティング(MediaTemple gs)では、1時間に多くのリクエストを実行できた可能性があります。
アカウントで認証して 350 リクエストの制限を取得できることはわかっていますが、まだテストしていませんが、これで問題がまったく解決するとは思いません。
10分ごとに実行するcronは次のとおりです。
<?php
//Set local timezone
putenv("TZ=America/Caracas");
//Function to get contents using cURL
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
//The path of the file that contains cached tweets
$cache = '/home/xxxxx/domains/example.com/html/demos/webname/twitter-json.txt';
//Call the api and get JSON response
$data = url_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=webname&count=100");
$parsed = json_decode($data, true);
//This is a workaround I made to exit the script if there's an error(it works)
if ($parsed['error']) exit;
//I change the twitter date format to d/m/y H:i
foreach ($parsed as $key => $value) {
$parsed[$key]['created_at'] = date('d/m/y H:i',strtotime($value['created_at']));
}
//I encode to JSON
$data = json_encode($parsed);
//Save the file
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,$data);
fclose($cachefile);
?>