私は現在、phpとangularを使ってTwitterから返されたデータをキャッシュしています。私は私のPHPファイルで返されるデータをキャッシュしていますAPIの制限を超えないようにするためにif/else文で関数を使うphpが動作しない
私はそれがif/else
文で繰り返さように、私は再利用できる機能に次のように入れしようとしています
<?php
require_once('twitter_proxy.php');
// Twitter OAuth Config options
$oauth_access_token = '*****';
$oauth_access_token_secret = '*****';
$consumer_key = '*****';
$consumer_secret = '*****';
$user_id = '*****';
$screen_name = 'StackOverflow';
$count = 5;
$twitter_url = 'statuses/user_timeline.json';
$twitter_url .= '?user_id=' . $user_id;
$twitter_url .= '&screen_name=' . $screen_name;
$twitter_url .= '&count=' . $count;
// Create a Twitter Proxy object from our twitter_proxy.php class
$twitter_proxy = new TwitterProxy(
$oauth_access_token, // 'Access token' on https://apps.twitter.com
$oauth_access_token_secret, // 'Access token secret' on https://apps.twitter.com
$consumer_key, // 'API key' on https://apps.twitter.com
$consumer_secret, // 'API secret' on https://apps.twitter.com
$user_id, // User id (http://gettwitterid.com/)
$screen_name, // Twitter handle
$count // The number of tweets to pull out
);
//check if the file exists
if(!file_exists('twitter_result.json')) {
// Invoke the get method to retrieve results via a cURL request
$tweets = $twitter_proxy->get($twitter_url);
//create a file with timestamp containing tweets
$data = array ('twitter_result' => $tweets, 'timestamp' => time());
file_put_contents('twitter_result.json', json_encode($data));
}else {
//if file exists check it has not been updated in 10 minutes
//if not update the tweets and timestamp
$data = json_decode(file_get_contents('twitter_result.json'));
if ($data->{"timestamp"} > (time() - 10 * 60)) {
// Invoke the get method to retrieve results via a cURL request
$tweets = $twitter_proxy->get($twitter_url);
$data = array ('twitter_result' => $tweets, 'timestamp' => time());
file_put_contents('twitter_result.json', json_encode($data));
}
}
?>
。しかし、私がif/else
ステートメントに次のコードを記述すると、動作しません。
function checkForUpdates() {
$tweets = $twitter_proxy->get($twitter_url);
$data = array ('twitter_result' => $tweets, 'timestamp' => time());
file_put_contents('twitter_result.json', json_encode($data));
}
$twitter_url
と$twitter_proxy
は、関数のスコープで定義されていない:あなたはvariable scope問題を抱えている
//check if the file exists
if(!file_exists('twitter_result.json')) {
checkForUpdates();
}else {
//if file exists check it has not been updated in 10 minutes
//if not update the tweets and timestamp
$data = json_decode(file_get_contents('twitter_result.json'));
if ($data->{"timestamp"} > (time() - 10 * 60)) {
checkForUpdates();
}
}
まだ動作していません!それは$ twitter_proxyまたは$ twitter_urlが 'if/else'ステートメントにあるかどうか分からないようです。それは意味をなさないでしょうか? –
@phantom私の編集を見て、あなたはパラメータで関数を呼び出しましたか? – jeroen
私は持っていると思っていましたが、今は動作します!ありがとう! –