Giter Club home page Giter Club logo

fuel-twitter's Introduction

Fuel Twitter Package

A super simple Twitter package for Fuel. This is based off of Elliot Haughin's CodeIgniter Twitter library.

About

Installation

Git Submodule

If you are installing this as a submodule (recommended) in your git repo root, run this command:

$ git submodule add git://github.com/dhorrigan/fuel-twitter.git fuel/packages/twitter/

Then you you need to initialize and update the submodule:

$ git submodule update --init fuel/packages/twitter/

Download

Alternatively you can download it and extract it into fuel/packages/twitter/.

Using Oil

$ php oil package install twitter

Configuration

Configuration is easy. First thing you will need to do is to register your app with twitter (if you haven't already) at https://dev.twitter.com/apps/new..

Next, copy the config/twitter.php from the package up into your app/config/ directory. Open it up and enter your API keys.

Note: It will use different keys for different environments by default.

Common Methods

Twitter::logged_in()

Simply checks if the current session is logged in through Twitter.

if (Twitter::logged_in())
{
	echo 'You are logged in!';
}

Twitter::login()

Starts the login process. Sends a request to the Twitter Oauth to log you in.

if ( ! Twitter::logged_in())
{
	Twitter::login();
}

Twitter::set_callback($url)

Sets the callback URL to use. This is the URL that Twitter will redirect the user to after their credentials have been verified on twitter.com.

Twitter::set_callback(Uri::create('twitter/callback'));

Twitter::get_tokens()

Gets all of the user's and app's Oauth tokens.

Twitter::get_tokens();

/*
Returns an array in the following format:
array(
	'consumer_key'    => '',
	'consumer_secret' => '',
	'access_key'      => '',
	'access_secret'   => '',
)
*/

Twitter::get($api_path, $args = array())

Makes a GET request to the Twitter API using the given API path and args. See https://dev.twitter.com/docs/api for the URLs.

// Verifies the user and returns all of the user's information
$twitter_user = Twitter::get('account/verify_credentials');

Twitter::post($api_path, $args = array())

Makes a POST request to the Twitter API using the given API path and args. See https://dev.twitter.com/docs/api for the URLs.

// Updates the current user's status (it Tweets)
$result = Twitter::post('statuses/update', array('status' => 'Using this new awesome cool Twitter package for Fuel!'));

Twitter::search($args)

Sends a request to the Twitter Search API with the given arguments.

// Gets all the tweets with the hashtag of #fuelphp
Twitter::search(array('q' => urlencode('#fuelphp')));

Example Login Controller:

<?php

class Controller_Twitter extends Controller {

	public function action_login()
	{
		if ( ! Twitter::logged_in() )
		{
			Twitter::set_callback(Uri::create('twitter/callback'));
			Twitter::login();
		}
		else
		{
			Response::redirect(Uri::create('/'));
		}
	}

	public function action_logout()
	{
		Session::destroy();
		Response::redirect(Uri::create('/'));
	}

	public function action_callback()
	{
		$tokens = Twitter::get_tokens();
		$twitter_user = Twitter::get('account/verify_credentials');

		// Update or create the user.  We update every time a user logs in
		// so that if they update their profile, we get that update.
		$user = Model_User::find_by_screen_name($twitter_user->screen_name);
		if ( ! $user)
		{
			$user = new Model_User();
		}
		$user->screen_name = $twitter_user->screen_name;
		$user->name = $twitter_user->name;
		$user->description = $twitter_user->description;
		$user->avatar = $twitter_user->profile_image_url;
		$user->oauth_token = $tokens['oauth_token'];
		$user->oauth_token_secret = $tokens['oauth_token_secret'];
		$user->save();
		
		Session::set('user_id', $user->id);
		
		Response::redirect(Uri::create('/'));
	}
	
}

fuel-twitter's People

Contributors

dhrrgn avatar frankdejonge avatar jesseobrien avatar karamage avatar

Stargazers

 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.