Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Last active July 7, 2018 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian-enspired/4536162 to your computer and use it in GitHub Desktop.
Save adrian-enspired/4536162 to your computer and use it in GitHub Desktop.
a class to access info from Twitter's "Sign in With Twitter" API. You can add additional methods as desired. See the conversation here: [http://css-tricks.com/forums/discussion/21825/output-value-of-nested-arrays]
<?php
/**
* wraps info returned from twitter's "sign in with twitter" API.
* add additional methods/properties as desired and use as a user class.
*
* @author Adrian Testa-Avila <github@custom-anything.com>
* @copyright 2013 Adrian Testa-Avila
* @license creative commons attribution-sharealike
* http://creativecommons.org/licenses/by-sa/3.0/
*
* @uses PHP 5.4+ For compatibility with v5.2+,
* see ## special instructions at|around line 42.
*/
class twitter_user{
## NONPUBLIC PROPERTIES ##
/**
* object this will hold the info you got from the twitter API.
* "protected" means it can't be accessed directly from outside the object
* (e.g., like `echo $twitter_user->t`).
* we do this so we can control how $t is accessed, using the __get() method below.
*/
protected $t;
## PUBLIC METHODS ##
/**
* this method creates a new twitter_user object -
* e.g., like `$chrisburton = new twitter_user( $twitter_array );`
* it simply converts $twitter_array to an object and stores it in $this->t.
* see the __get() method to see how you retrieve values.
*
* @param array $twitter_info the info you got from twitter.
*/
public function __construct( array $twitter_array ){
if( !empty( $twitter_array['auth'] ) && is_array( $twitter_array['auth'] ) ){
// you might want to verify other|all values exist,
// beyond just ['auth'], but that's up to you.
// converting the array into an object
// makes it easier to access the values via our __get() method.
// (using the json_* functions is an ugly hack, but works well.)
## this works with PHP version 5.4.
## v.5.3 does not support the json_encode flags used here (you can remove them).
## v.5.2 does not support json_last_error (you can remove the error check and hope for the best.)
## see comments on|around lines 51-62 below for alternate code if you need 5.2+ compatibility
## (however, UPGRADING to 5.4+ is the recommended solution).
$t = json_decode(
json_encode(
$twitter_array['auth']
## PHP 5.3 compatability:
## comment out the following line
,JSON_BIGINT_AS_STRING|JSON_UNESCAPED_UNICODE
)
);
// make sure there were no errors
## PHP 5.2 compatability:
## comment out the following line
if( json_last_error() === JSON_ERROR_NONE ){
## PHP 5.2 compatability:
## un-comment the following line
// if( true ){
// all good.
$this->t = $t;
}else{
// throw an exception so you know something went wrong
throw new Exception( 'conversion to object failed' );
}
}else{
// throw an exception so you know something went wrong
throw new Exception( '$twitter_info is malformed' );
}
}
/**
* __get() is a "magic method" that runs
* whenever you try to access a nonpublic (or nonexistant) property.
* we're going to use it to access the twitter array.
*
* @param string $name the name of the value asked for
* @return mixed the value that $name is mapped to, if it exists;
* FALSE otherwise
*/
public function __get( $name ){
// first check if $name maps directly to a value in the twitter array:
if( isset( $this->t->$name ) ){
// this handles usage like `echo $chrisburton->uid;`
return $this->t->$name;
}
// next, check if $name maps to a nested value:
foreach( $this->t as $t ){
if( isset( $t->$name ) ){
// this handles usage like `echo $chrisburton->nickname;`
// or even `echo $chrisburton->urls->twitter;`
// (but *not* `echo $chrisburton->twitter;` - that won't work).
return $t->$name;
}
}
// finally, if nothing was found:
return false;
}
}
## EXAMPLE USAGE ##
/* (REMOVE everything that follows in production use) */
/** returned from twitter oauth API. */
$twitter_array = array(
'auth' => array(
'uid' => 0
,'info' => array(
'name' => 'Chris Burton'
,'nickname' => 'chrisburton'
,'urls' => array(
'twitter' => 'http://twitter.com/chrisburton'
,'website' => 'http://chrisburton.me'
,'dribble' => 'http://dribbble.com/chrisburton'
)
,'location' => 'New York'
,'description' => 'Letterer & Typographer'
,'image' => 'http://a0.twimg.com/profile_images/3106804659/f100a994784f6e8059bb15125f286667_normal.png'
)
,'credentials' => array(
'token' => 'Token Removed'
,'secret' => 'Secret Removed'
)
,'raw' => array(
'id' => 0
,'profile_sidebar_fill_color' => 'DDEEF6'
,'created_at' => 'Thu Jun 25 22:48:35 +0000 2009'
,'contributors_enabled' => 0
)
)
);
try{
// create new twitter_user
$cb = new twitter_user( $twitter_array );
}catch( Exception $e ){
print $e->getMessage();
exit;
}
// try it out:
// maps to `twitter_user->t->uid`
// outputs "0"
print $cb->uid;
// maps to `twitter_user->t->info->name`
// outputs "Chris Burton"
print $cb->name;
// maps to `twitter_user->t->urls->twitter`
// outputs "http://twitter.com/chrisburton"
print $cb->urls->twitter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment