私はフロントエンドから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つでした! –
'success'と' error'をどのように置き換えるべきかを示す答えを更新できますか? –
@phantom、ここに行きます。 –