shahrukhkhan / twitter
Laravel的Twitter API
Requires
- php: >=5.4.0
- illuminate/support: 4.*|5.*
- themattharris/tmhoauth: 0.8.4
This package is auto-updated.
Last update: 2024-09-20 01:01:30 UTC
README
Laravel 4/5的Twitter API
您需要在应用管理中创建一个应用程序并创建您的访问令牌。
安装
将 shahrukhkhan/twitter 添加到 composer.json 文件中。
"shahrukhkhan/twitter": "~2.0"
运行 composer update 以拉取Twitter的最新版本。
或者运行
composer require shahrukhkhan/twitter
现在打开 /config/app.php 并将服务提供者添加到您的 providers 数组中。
'providers' => [ 'Shahrukh\Twitter\TwitterServiceProvider', ]
现在添加别名。
'aliases' => [ 'Twitter' => 'Shahrukh\Twitter\Facades\Twitter', ]
从1.x.x升级
该软件包现在需要PHP >= 5.4.0
外观已更改(Shahrukh\Twitter\Facades\Twitter)
配置文件已更新(debug, UPLOAD_URL, ACCESS_TOKEN_URL, REQUEST_TOKEN_URL)
set_new_config() 已重命名为 reconfig()
配置(Laravel 4)
运行 php artisan config:publish shahrukhkhan/twitter 并使用您的信息修改配置文件。
/app/config/packages/shahrukh/twitter/config.php
同时,确保从配置文件中删除.env并在其中替换您的信息。
配置(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' => 'yebhidekho', '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);