Give help. Get help.

  • # January 15, 2013 at 12:25 am

    @traq No, I’m using 5.3

    Edit: I already changed that to $response.

    # January 15, 2013 at 12:28 am

    I updated my demo to be compatible with PHP 5.3. Sorry about that. You can try it again.

    In your own script, did you *completely remove* my demo code (everything below `## EXAMPLE USAGE ##`) and replace it with

    $cb = new twitter_user( $response );


    # January 15, 2013 at 12:37 am

    @traq I believe so?

    * 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
    * @copyright 2013 Adrian Testa-Avila
    * @license creative commons attribution-sharealike
    class twitter_user{


    * 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;


    * this method creates a new twitter_user object –
    * e.g., like `$chrisburton = new twitter_user( $response );`
    * it simply converts $response 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 $response ){
    if( !empty( $response ) && is_array( $response ) ){
    // you might want to verify other|all values exist,
    // beyond just , 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.)
    $t = json_decode(
    // make sure there were no errors
    if( json_last_error() === JSON_ERROR_NONE ){
    // all good.
    $this->t = $t;
    // throw an exception so you know something went wrong
    throw new Exception( ‘conversion to object failed’ );
    // 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;

    $cb = new twitter_user( $response );

    # January 15, 2013 at 12:42 am


    …and `$response` contains the array from twitter (the array you posted at the beginning of this thread)?

    are you still getting the “unexpected [” error?

    # January 15, 2013 at 12:47 am

    @traq Yes, $response is the main array or whatever you call it.

    I’m getting two errors on line 189 which is `$cb = new twitter_user( $twitter_array );`

    Notice: Undefined variable: twitter_array in C:UsersChrisDesktopwampwwwauthexamplecallback.php on line 189

    Catchable fatal error: Argument 1 passed to twitter_user::__construct() must be an array, null given, called in C:UsersChrisDesktopwampwwwauthexamplecallback.php on line 189 and defined in C:UsersChrisDesktopwampwwwauthexamplecallback.php on line 131

    Line 131 is: `public function __construct( array $response ){`

    # January 15, 2013 at 12:51 am

    > I’m getting two errors on line 189 which is $cb = new twitter_user( $twitter_array );

    Change that to `$cb = new twitter_user( $response );`

    If you still get an error, use `var_dump( $response )` to make sure $response really is the array we’re looking for :)

    # January 15, 2013 at 12:57 am

    @traq Oops, my fault. Now I’m getting a ton of json errors. Will try the `var_dump`

    # January 15, 2013 at 1:00 am

    @traq `$response` is correct. It outputs the following:

    array (size=3)
    ‘auth’ =>
    array (size=5)
    ‘uid’ => int 50799167
    ‘info’ =>
    array (size=6)
    ‘name’ => string ‘Chris Burton’ (length=12)
    ‘nickname’ => string ‘chrisburton’ (length=11)
    ‘urls’ =>
    array (size=2)…

    # January 15, 2013 at 1:17 am

    can you zip it all to me?

    # January 15, 2013 at 1:30 am

    @traq Sent. Thanks again.

    # January 15, 2013 at 10:06 am


    Try putting this

    // create new twitter_user
    $cb = new twitter_user( $response );
    print “Hi, my name is {$cb->name}.
    Visit me at {$cb->urls->website}”;

    *inside* the `else{}` block where it says

    * It’s all good. Go ahead with your application-specific authentication logic

    I’ll look more in-depth this afternoon.

    # January 15, 2013 at 1:13 pm

    @traq Hey, no rush. Unless I’m doing something wrong, I’m still getting an error.

    # January 15, 2013 at 3:59 pm


    no bother; I’m interested in this too (the whole Opauth/twitter thing).

    Are your errors still the “undefined variable” and “catchable fatal error” on 189? Nothing else?

    And have you tried the twitter_user class again (get a fresh copy, since I fixed the demo), *by itself*? (Just trying to narrow it down; the class demo works for me, without a hitch.)

    # January 16, 2013 at 1:09 am

    Okay, I’ve discovered the problem with my [twitter_user class](, and provided a solution.

    (1) The flags I use for [json_encode()]( aren’t supported in PHP versions less-than 5.4.

    … The solution is to remove the flags.

    (2) The function [json_last_error()]( did not exist in PHP versions less-than 5.3.

    … The “solution” (and I use the term loosely) is to remove the error-check and “hope for the best.” A better solution is to **upgrade your PHP version**.


    You say you’re using 5.3, correct? Simply commenting out this line


    should solve your problem. If not, you can also replace this line

    if( json_last_error() === JSON_ERROR_NONE ){

    with this instead

    if( true ){

    I have a [working example here](, if you’d like to see the result.

    # January 16, 2013 at 1:25 am

    @traq Wow. I checked my host’s PHP version and I’m using `PHP Version 5.2.17`. I upgraded my localhost version to 5.3 due to a certain feature that gave me an error.

    Really appreciate all of this. After reading some articles, I keep seeing `if(isset())` being used for authenticating a twitter user. I’m concerned that I should have stuck with that or is this completely separate?

Viewing 15 posts - 16 through 30 (of 39 total)

You must be logged in to reply to this topic.