rummykhan / twitter
Laravel 的 Twitter API
Requires
- php: >=5.4.0
- illuminate/support: 4.*|5.*
- themattharris/tmhoauth: 0.8.4
This package is not auto-updated.
Last update: 2024-09-20 18:34:20 UTC
README
Laravel 4/5 的 Twitter API
您需要在 应用程序管理 中创建应用程序并创建您的访问令牌。
安装
将 thujohn/twitter
添加到 composer.json
。
"thujohn/twitter": "~2.0"
运行 composer update
以获取 Twitter 的最新版本。
或者运行
composer require thujohn/twitter
现在打开 /config/app.php
并将服务提供者添加到您的 providers
数组中。
'providers' => [ 'Thujohn\Twitter\TwitterServiceProvider', ]
现在添加别名。
'aliases' => [ 'Twitter' => 'Thujohn\Twitter\Facades\Twitter', ]
从 1.x.x 升级
该包现在需要 PHP >= 5.4.0
外观已更改(Thujohn\Twitter\Facades\Twitter)
配置文件已更新(debug, UPLOAD_URL, ACCESS_TOKEN_URL, REQUEST_TOKEN_URL)
set_new_config() 已重命名为 reconfig()
配置(Laravel 4)
运行 php artisan config:publish thujohn/twitter
并使用您自己的信息修改配置文件。
/app/config/packages/thujohn/twitter/config.php
此外,请确保在配置文件中删除环境变量,并用您自己的信息替换它。
配置(Laravel 5)
运行 php artisan vendor:publish
并使用您自己的信息修改配置文件。
/config/ttwitter.php
在 Laravel 5 中,编辑 config.php 文件非常简单 - 事实上,您甚至不需要触摸它!只需将以下内容添加到您的 .env 文件中即可
TWITTER_CONSUMER_KEY =
TWITTER_CONSUMER_SECRET =
TWITTER_ACCESS_TOKEN =
TWITTER_ACCESS_TOKEN_SECRET =
特殊参数
format : object|json|array (default:object)
函数
Linkify:将 URL、@用户名、标签转换为链接。$tweet 的类型可以是对象、数组或文本。通过发送对象或数组,该方法还将扩展链接(t.co)。
Twitter::linkify($tweet);
Ago:将日期转换为差异(2小时前)
Twitter::ago($timestamp);
LinkUser:通过用户对象(例如 $tweet->user)或 id/string 生成指向特定用户的链接。
Twitter::linkUser($user);
LinkTweet:生成指向特定推文的链接。
Twitter::linkTweet($tweet);
示例
返回由 screen_name 或 user_id 参数指定的用户最近发布的推文的集合。
Route::get('/', function() { return Twitter::getUserTimeline(['screen_name' => 'thujohn', 'count' => 20, 'format' => 'json']); });
返回由认证用户及其关注用户最近发布的推文和转发的集合。
Route::get('/', function() { return Twitter::getHomeTimeline(['count' => 20, 'format' => 'json']); });
返回认证用户的 X 个最新提及(包含用户 @screen_name 的推文)。
Route::get('/', function() { return Twitter::getMentionsTimeline(['count' => 20, 'format' => 'json']); });
更新认证用户的当前状态,也称为发推。
Route::get('/', function() { return Twitter::postTweet(['status' => 'Laravel is beautiful', 'format' => 'json']); });
使用媒体更新认证用户的当前状态。
Route::get('/', function() { $uploaded_media = Twitter::uploadMedia(['media' => File::get(public_path('filename.jpg'))]); return Twitter::postTweet(['status' => 'Laravel is beautiful', 'media_ids' => $uploaded_media->media_id_string]); });
通过 Twitter 登录
Route::get('twitter/login', ['as' => 'twitter.login', function(){ // your SIGN IN WITH TWITTER button should point to this route $sign_in_twitter = true; $force_login = false; // Make sure we make this request w/o tokens, overwrite the default values in case of login. Twitter::reconfig(['token' => '', 'secret' => '']); $token = Twitter::getRequestToken(route('twitter.callback')); if (isset($token['oauth_token_secret'])) { $url = Twitter::getAuthorizeURL($token, $sign_in_twitter, $force_login); Session::put('oauth_state', 'start'); Session::put('oauth_request_token', $token['oauth_token']); Session::put('oauth_request_token_secret', $token['oauth_token_secret']); return Redirect::to($url); } return Redirect::route('twitter.error'); }]); Route::get('twitter/callback', ['as' => 'twitter.callback', function() { // You should set this route on your Twitter Application settings as the callback // https://apps.twitter.com/app/YOUR-APP-ID/settings if (Session::has('oauth_request_token')) { $request_token = [ 'token' => Session::get('oauth_request_token'), 'secret' => Session::get('oauth_request_token_secret'), ]; Twitter::reconfig($request_token); $oauth_verifier = false; if (Input::has('oauth_verifier')) { $oauth_verifier = Input::get('oauth_verifier'); } // getAccessToken() will reset the token for you $token = Twitter::getAccessToken($oauth_verifier); if (!isset($token['oauth_token_secret'])) { return Redirect::route('twitter.login')->with('flash_error', 'We could not log you in on Twitter.'); } $credentials = Twitter::getCredentials(); if (is_object($credentials) && !isset($credentials->error)) { // $credentials contains the Twitter user object with all the info about the user. // Add here your own user logic, store profiles, create new users on your tables...you name it! // Typically you'll want to store at least, user id, name and access tokens // if you want to be able to call the API on behalf of your users. // This is also the moment to log in your users if you're using Laravel's Auth class // Auth::login($user) should do the trick. Session::put('access_token', $token); return Redirect::to('/')->with('flash_notice', 'Congrats! You\'ve successfully signed in!'); } return Redirect::route('twitter.error')->with('flash_error', 'Crab! Something went wrong while signing you up!'); } }]); Route::get('twitter/error', ['as' => 'twitter.error', function(){ // Something went wrong, add your own error handling here }]); Route::get('twitter/logout', ['as' => 'twitter.logout', function(){ Session::forget('access_token'); return Redirect::to('/')->with('flash_notice', 'You\'ve successfully logged out!'); }]);
调试
首先在配置文件中激活调试。
然后您可以通过 logs() 方法访问。
try { $response = Twitter::getUserTimeline(['count' => 20, 'format' => 'array']); } catch (Exception $e) { dd(Twitter::logs()); } dd($response);