2016-05-31 2 views
0

私は現在、phpとangularを使ってTwitterから返されたデータをキャッシュしています。私は私のPHPファイルで返されるデータをキャッシュしていますAPIの制限を超えないようにするためにif/else文で関数を使うphpが動作しない

私はそれがif/else文で繰り返さように、私は再利用できる機能に次のように入れしようとしています

<?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ステートメントに次のコードを記述すると、動作しません。

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は、関数のスコープで定義されていない:あなたはvariable scope問題を抱えている

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

答えて

1

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文は次のようなものを見てみたいです。

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); 
+0

まだ動作していません!それは$ twitter_proxyまたは$ twitter_urlが 'if/else'ステートメントにあるかどうか分からないようです。それは意味をなさないでしょうか? –

+0

@phantom私の編集を見て、あなたはパラメータで関数を呼び出しましたか? – jeroen

+0

私は持っていると思っていましたが、今は動作します!ありがとう! –

0

を問題は、あなたの関数である$の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); 
    } 
} 
関連する問題