2017 © Pedro Peláez
 

library twitter

Twitter API for Laravel

image

shahrukhkhan/twitter

Twitter API for Laravel

  • Wednesday, August 9, 2017
  • by khanof89
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 100 % Grown

The README.md

Twitter

Twitter API for Laravel 4/5, (*1)

You need to create an application and create your access token in the Application Management., (*2)

Build Status, (*3)

Installation

Add shahrukhkhan/twitter to composer.json., (*4)

"shahrukhkhan/twitter": "~2.0"

Run composer update to pull down the latest version of Twitter., (*5)

Or run, (*6)

composer require shahrukhkhan/twitter

Now open up /config/app.php and add the service provider to your providers array., (*7)

'providers' => [
    'Shahrukh\Twitter\TwitterServiceProvider',
]

Now add the alias., (*8)

'aliases' => [
    'Twitter' => 'Shahrukh\Twitter\Facades\Twitter',
]

Upgrading from 1.x.x

The package now requires PHP >= 5.4.0, (*9)

Facade has changed (Shahrukh\Twitter\Facades\Twitter), (*10)

Config file has been updated (debug, UPLOAD_URL, ACCESS_TOKEN_URL, REQUEST_TOKEN_URL), (*11)

set_new_config() has been renamed reconfig(), (*12)

Configuration (Laravel 4)

Run php artisan config:publish shahrukhkhan/twitter and modify the config file with your own informations., (*13)

/app/config/packages/shahrukh/twitter/config.php

Also, make sure to remove the env in the config file and replace it with your information., (*14)

Configuration (Laravel 5)

Run php artisan vendor:publish and modify the config file with your own information., (*15)

/config/ttwitter.php

With Laravel 5, it's simple to edit the config.php file - in fact you don't even need to touch it! Just add the following to your .env file and you'll be on your way:, (*16)

TWITTER_CONSUMER_KEY = 
TWITTER_CONSUMER_SECRET = 
TWITTER_ACCESS_TOKEN = 
TWITTER_ACCESS_TOKEN_SECRET =

Special parameter

format : object|json|array (default:object)

Functions

Linkify : Transforms URLs, @usernames, hashtags into links. The type of $tweet can be object, array or text. By sending an object or an array the method will expand links (t.co) too., (*17)

Twitter::linkify($tweet);

Ago : Converts date into difference (2 hours ago), (*18)

Twitter::ago($timestamp);

LinkUser : Generates a link to a specific user, by their user object (such as $tweet->user), or id/string., (*19)

Twitter::linkUser($user);

LinkTweet : Generates a link to a specific tweet., (*20)

Twitter::linkTweet($tweet);

Examples

Returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters., (*21)

Route::get('/', function()
{
    return Twitter::getUserTimeline(['screen_name' => 'yebhidekho', 'count' => 20, 'format' => 'json']);
});

Returns a collection of the most recent Tweets and retweets posted by the authenticating user and the users they follow., (*22)

Route::get('/', function()
{
    return Twitter::getHomeTimeline(['count' => 20, 'format' => 'json']);
});

Returns the X most recent mentions (tweets containing a users's @screen_name) for the authenticating user., (*23)

Route::get('/', function()
{
    return Twitter::getMentionsTimeline(['count' => 20, 'format' => 'json']);
});

Updates the authenticating user's current status, also known as tweeting., (*24)

Route::get('/', function()
{
    return Twitter::postTweet(['status' => 'Laravel is beautiful', 'format' => 'json']);
});

Updates the authenticating user's current status with media., (*25)

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]);
});

Sign in with twitter, (*26)

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!');
}]);

Debug

First activate debug in the config file., (*27)

Then you can access the logs() method., (*28)

try
{
    $response = Twitter::getUserTimeline(['count' => 20, 'format' => 'array']);
}
catch (Exception $e)
{
    dd(Twitter::logs());
}

dd($response);

The Versions

09/08 2017

dev-master

9999999-dev

Twitter API for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar thujohn
by shahrukh

laravel laravel4 twitter laravel5

24/02 2016

1.0.0

1.0.0.0

Twitter API for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar thujohn

laravel laravel4 twitter laravel5