對於在類似情況下別人:我覺得最簡單的只寫我自己的函數,而不是使用第二個庫。
舉例路線: Route::post('tweet', ['middleware' => 'auth', 'uses' => '[email protected]']);
例控制器功能: public function searchTweets(Request $request) { // $url = 'https://api.twitter.com/1.1/search/tweets.json'; $client = new GuzzleHttp\Client(); $user = User::find($request['user']['sub']); $profileOauth = new Oauth1([ 'consumer_key' => Config::get('app.twitter_key'), 'consumer_secret' => Config::get('app.twitter_secret'), 'token' => $user->oauthToken, 'token_secret' => $user->oauthVerifier ]); $client->getEmitter()->attach($profileOauth); $response = $client->get($url, ['auth' => 'oauth', 'query' => ['q' => $request->input('q'), 'result_type' => $request->input('result_type'), #'since_id' => $request->input('since_id', 0), #'max_id' => $request->input('max_id', 0), 'count' => $request->input('count')] ])->json(); return $response; }
這是我angularJS嘰嘰喳喳廠: var vm = {}; vm.tweets = []; vm.busy = false; vm.outreaches = 0; vm.retweets = 0; vm.favourites = 0; vm.followed = 0; vm.status = ''; vm.since_id = null; vm.until = null; vm.max_id = null; vm.count = 100; vm.q = ''; vm.result_type = 'mixed'; /* * mixed: Include both popular and real time results in the response. * recent: return only the most recent results in the response * popular: return only the most popular results in the response. */ vm.searchTweets = function() { if (vm.busy) { return; } vm.busy = true; var url = "/twitter/search"; $http({ url: url, method: "GET", params: { q: vm.q, result_type: vm.result_type || 'mixed', count: vm.count || 100, since_id: vm.since_id || null, //until: vm.until || null, max_id: vm.max_id || null } }).then(function (response) { var newTweets = response.data.statuses; console.log('newTweets: '); console.log(newTweets); for (var i = 0; i < newTweets.length; i++) { vm.tweets.push(newTweets[i]); } vm.since_id = vm.tweets[vm.tweets.length - 1].id_str; vm.busy = false; }, function (response) { // called asynchronously if an error occurs // or server returns response with an error status. console.log(response); angular.forEach(response.data.errors, function(value, key){ $window.Materialize.toast(value.message, 3000); }); }); };