Giter VIP home page Giter VIP logo

twitter-l4's Introduction

Twitter

Twitter API for Laravel 4

You need to create an application and create your access token in the developer console.

Build Status

Installation

Add thujohn/twitter to composer.json.

"thujohn/twitter": "dev-master"

Run composer update to pull down the latest version of Twitter.

Now open up app/config/app.php and add the service provider to your providers array.

'providers' => array(
	'Thujohn\Twitter\TwitterServiceProvider',
)

Now add the alias.

'aliases' => array(
	'Twitter' => 'Thujohn\Twitter\TwitterFacade',
)

Configuration

Run php artisan config:publish thujohn/twitter and modify the config file with your own informations.

/app/config/packages/thujohn/twitter/config.php

Special parameter

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

Functions

Linkify : Transforms URLs, @usernames, hashtags into links

Twitter::linkify($tweet);

Ago : Converts date into difference (2 hours ago)

Twitter::ago($timestamp);

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

Twitter::linkUser($user);

LinkTweet : Generates a link to a specific tweet.

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.

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

Returns a collection of the most recent Tweets and retweets posted by the authenticating user and the users they follow.

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

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

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

Updates the authenticating user's current status, also known as tweeting.

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

Sign in with twitter

Route::get('/twitter/login', function()
{
	// your SIGN IN WITH TWITTER  button should point to this route
	$sign_in_twitter = TRUE;
	$force_login = FALSE;
	$callback_url = 'http://' . $_SERVER['HTTP_HOST'] . '/twitter/callback';
	// Make sure we make this request w/o tokens, overwrite the default values in case of login.
	Twitter::set_new_config(array('token' => '', 'secret' => ''));
	$token = Twitter::getRequestToken($callback_url);
	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::to('twitter/error');
});

Route::get('/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 = array(
			'token' => Session::get('oauth_request_token'),
			'secret' => Session::get('oauth_request_token_secret'),
		);

		Twitter::set_new_config($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::to('/')->with('flash_error', 'We could not log you in on Twitter.');
		}

		$credentials = Twitter::query('account/verify_credentials');
		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.

			return Redirect::to('/')->with('flash_notice', "Congrats! You've successfully signed in!");
		}
		return Redirect::to('/')->with('flash_error', 'Crab! Something went wrong while signing you up!');
	}
});

Route::get('twitter/error', function(){
	// Something went wrong, add your own error handling here
});

twitter-l4's People

Contributors

anahkiasen avatar clarkf avatar djug avatar j3j5 avatar marcorivm avatar maumagau avatar nhalloran avatar nivv avatar nwidart avatar robertdeniszczyc avatar sdebruyn avatar thujohn avatar trapvincenzo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.