mardi 31 mai 2016

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 http://ift.tt/1oHSTpv
    $oauth_access_token_secret,     // 'Access token secret' on http://ift.tt/1oHSTpv
    $consumer_key,                  // 'API key' on http://ift.tt/1oHSTpv
    $consumer_secret,               // 'API secret' on http://ift.tt/1oHSTpv
    $user_id,                       // User id (http://ift.tt/1lBV9ME)
    $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();
        }
    }

Aucun commentaire:

Enregistrer un commentaire