简体   繁体   中英

Using function in if/else statement php not working

I am currently caching data returned from twitter using php and angular.

In order to prevent exceeding the API limit I am caching the data returned in my php file

<?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));
        }
    }

?>

I am trying to put the following into function so I can reuse as it repeats itself in the if/else statement. However, when I put the following code in the if/else statement it doesn't work.

function checkForUpdates() {
    $tweets = $twitter_proxy->get($twitter_url);
    $data = array ('twitter_result' => $tweets, 'timestamp' => time());
    file_put_contents('twitter_result.json', json_encode($data));
}

I want the if/else statement to look something like this:

    //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();
        }
    }

You have a variable scope problem:

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 and $twitter_proxy are not defined in the scope of the function. You should send them as parameters:

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));
}

And then you call your function where you need it like:

checkForUpdates($twitter_proxy, $twitter_url);

The problem is your function doesn't have access to the $twitter_proxy object, or to the $twitter_url string. One solution would be to switch the whole thing to a more Object Oriented Approach, but if you don't want to go that route you could just pass what you need to the function as parameters :

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));
}

so your function telles you "I need a twitter proxy and a URL", and you'll pass them whenever you call that function like so :

//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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM