[英]Using function in if/else statement php not working
我目前正在使用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();
}
}
您有一个可变范围问题:
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);
问题是您的函数无法访问$ 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.