目前,我正在使用php和angular缓存从twitter返回的数据。
为了防止超过API限制,我缓存了php文件中返回的数据。
<?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
语句中重复使用它。但是,当我将下面的代码放在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));
}
我希望if/else
语句看起来像这样:
//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();
}
}
发布于 2016-05-31 03:31:10
您有一个可变范围问题:
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
。您应该将它们作为参数发送:
function checkForUpdates($twitter_proxy, $twitter_url) {
$tweets = $twitter_proxy->get($twitter_url);
$data = array ('twitter_result' => $tweets, 'timestamp' => time());
file_put_contents('twitter_result.json', json_encode($data));
}
然后在需要它的地方调用函数,比如:
checkForUpdates($twitter_proxy, $twitter_url);
发布于 2016-05-31 03:36:27
问题是您的函数无法访问$twitter_proxy对象或$twitter_url字符串。一种解决方案是将整个过程转换为一种更面向对象的方法,但是如果您不想走这条路线,您只需将所需的内容作为参数传递给函数:
function checkForUpdates($twitter_proxy, $twitter_url) {
$tweets = $twitter_proxy->get($twitter_url);
$data = array ('twitter_result' => $tweets, 'timestamp' => time());
file_put_contents('twitter_result.json', json_encode($data));
}
因此,您的函数告诉您“我需要一个twitter代理和一个URL",每当您像这样调用该函数时,您就会传递它们:
//check if the file exists
if(!file_exists('twitter_result.json')) {
checkForUpdates($twitter_proxy, $twitter_url);
}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);
}
}
https://stackoverflow.com/questions/37544391
复制相似问题