2016-05-31 8 views
0

私はフロントエンドからphpファイルを呼び出しています。PHPファイルの実行終了後にJSONファイルへのGETリクエスト

phpファイルは10分ごとにつぶやきをチェックし、この時間が経過するとjsonファイルが更新され、その後angleから呼び出されます。私は2つの方法で角張った工場を持っています。 updateTwitter()と呼ばれる最初のメソッドは、GETというPHPファイルへのリクエストを行い、TwitterへのGETリクエストを行い、つぶやきを返します。これらはJSONファイルに書き込まれます。 2番目のメソッドfetchTwitter()は、このJSONファイルに対してGET要求を行い、フロントエンドにデータを表示します。

私の質問はどのように私のコントローラでこれらのメソッドを呼び出す必要があります。最初のメソッド(updateTwitter()) has finished don't run fecthTwitter() `)まで、私は約束を使い、何かを言わなければならないと感じています。これは正しいアプローチですか、もしそうなら、私はこれをどのように書きますか?

My code looksこのような。

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

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

//check if the file exists 
if(!file_exists('twitter_result.json')) { 
    //Invoke the get method to retrieve results via a cURL request 
    //and create a file with timestamp containing tweets 
    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); 
    } 
} 

角度コード

factory.js

//factory used to make GET request to Instagram 
app.factory('socialMedia', ['$http', function($http){ 
return { 

    updateTwitter: function() { 
     return $http({ 
      url: 'get_tweets.php', 
      method: 'GET' 
     }) 
    }, 
    fetchTwitter: function() { 
     return $http({ 
      url: 'twitter_result.json', 
      method: 'GET' 
     }) 
    } 
} 

controller.js

app.controller("testCtrl", ["$routeParams", "socialMedia", function ($routeParams, socialMedia) { 

    testCtrl = this; 
    this.twitterPosts = []; 

    socialMedia.updateTwitter(); 

    socialMedia.fetchTwitter().success(function(data){ 
     socialMedia.updateTwitter(); 
     var result = JSON.parse(data.twitter_result); 
     for(var i = 0; i < result.length; i++){ 
      testCtrl.twitterPosts.push(result[i]); 
     }  
    }) 
    .error(function() { 
     testCtrl.loading = false; 
     testCtrl.error = true; 
    }); 
}]); 

答えて

1

$http.get方法は、簡単にこのようなあなたの呼び出しをチェーンすることができます約束を返すので:

socialMedia.updateTwitter() 
.then(function() { 
    socialMedia.fetchTwitter() 
    .then(function(data){ 
     ... 
    }) 
}); 

$http documentationによると、注意してください。

$ http従来の約束方法successerrorは推奨されていません。したがって

に代わり、標準 thenメソッドを使用し、あなたのコントローラは次のようになります。

app.controller("testCtrl", ["$routeParams", "socialMedia", function ($routeParams, socialMedia) { 
    testCtrl = this; 
    this.twitterPosts = []; 

    socialMedia.updateTwitter() 
    .then(function(){ 
     socialMedia.fetchTwitter() 
     .then(function(response){ 
      var data = response.data; 
      var result = JSON.parse(data.twitter_result); 
      for(var i = 0; i < result.length; i++){ 
       testCtrl.twitterPosts.push(result[i]); 
      }  
     }) 
     .catch(function(error){ 
      testCtrl.loading = false; 
      testCtrl.error = true; 
     }); 
    }); 
}]); 
+0

歓声アレック、それは1つでした! –

+0

'success'と' error'をどのように置き換えるべきかを示す答えを更新できますか? –

+0

@phantom、ここに行きます。 –

0

はい、それは良い方法だろう。最初のコールバックとして2番目のリクエストを実行するか、または約束の.then()メソッドを使用して2番目のリクエストをチェーンします。 しかし、別の2番目のリクエストを行う代わりに、最初のリクエストのレスポンスでデータを渡すのはなぜですか?

+0

質問に質問に答えないようにしてください。コメントがより適切でしょう。 – Pogrindis

関連する問題