我从角度前端调用一个php文件,然后显示推文的时间线。
php文件每十分钟检查一次推文,然后更新一个json文件,然后从angular调用。我想创建一个管理员功能来标记/隐藏时间线中显示的帖子。有没有人对如何做到这一点有任何建议?
是否可以通过从Angular向php文件发出POST
请求来设置推文上的标志来完成?如果是这样,怎么办呢?
我的代码看起来像这样。
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
app.factory('testMedia', ['$http', function($http){
return {
updateTwitter: function() {
return $http({
url: 'get_tweets.php',
method: 'GET'
})
},
fetchTwitter: function() {
return $http({
url: 'twitter_result.json',
method: 'GET'
})
}
}
}]);
twitterCtrl.js
app.controller("twitterCtrl", ["$routeParams", "testMedia", function ($routeParams, socialMedia) {
twitterCtrl = this;
this.twitterPosts = [];
testMedia.updateTwitter()
.then(function(){
testMedia.fetchTwitter()
.then(function(response){
var result = JSON.parse(response.data.twitter_result);
for(var i = 0; i < result.length; i++){
result[i].created_at = new Date(result[i].created_at);
twitterCtrl.twitterPosts.push(result[i]);
}
})
});
}]);