首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在if/ not语句php中使用函数不起作用

在if/ not语句php中使用函数不起作用
EN

Stack Overflow用户
提问于 2016-05-31 11:25:06
回答 2查看 84关注 0票数 0

目前,我正在使用php和angular缓存从twitter返回的数据。

为了防止超过API限制,我缓存了php文件中返回的数据。

代码语言:javascript
运行
复制
<?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语句中时,它无法工作。

代码语言:javascript
运行
复制
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语句看起来像这样:

代码语言:javascript
运行
复制
    //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();
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-31 11:31:10

您有一个可变范围问题:

代码语言:javascript
运行
复制
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。您应该将它们作为参数发送:

代码语言:javascript
运行
复制
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));
}

然后在需要它的地方调用函数,比如:

代码语言:javascript
运行
复制
checkForUpdates($twitter_proxy, $twitter_url);
票数 1
EN

Stack Overflow用户

发布于 2016-05-31 11:36:27

问题是您的函数无法访问$twitter_proxy对象或$twitter_url字符串。一种解决方案是将整个过程转换为一种更面向对象的方法,但是如果您不想走这条路线,您只需将所需的内容作为参数传递给函数:

代码语言:javascript
运行
复制
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",每当您像这样调用该函数时,您就会传递它们:

代码语言:javascript
运行
复制
//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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37544391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档