Welcome Guest, Not a member yet? Register   Sign In
Haughin’s Twitter Library - Accessing the current user's username.
#1

[eluser]clayko[/eluser]
Hey Everyone,

I'm a relative noob at codeigniter, but I have made some significant progress over the past 6 months.

I'm now facing a seemingly simple problem compared to the other problems I have solved while learning the twitter API. I cannot figure out how to access the user's username with Haughin's oAuth twitter API.

If anyone could help, I would be very appreciative.

Thanks a lot,
Clay
#2

[eluser]sheellli[/eluser]
Hi everyone ,

Even i m stucked in this twitter Api implementation.

While a user coming back from twitter,

The user_id is lost.which i have stored in an session.

Please guide me how to get it.
#3

[eluser]clayko[/eluser]
Can anyone please help?
#4

[eluser]luisfmgoncalves[/eluser]
Hey,

I'm not sure if I will anwser the right question, but I hope so...

For me is like, after a user login, I store his twitter id in the session. So, when I need to get the use information from twitter I just do:

Code:
$id = $this->session->userdata('id');
$user_info = $this->twitter->call('users/show', array('id' => $id));

With this, I have the JSON information in $user_info variable.

After that, to retrieve the information I just do:
Code:
if($user_info != NULL){
   $data["name"] = $user_info->name;
   $data["web"] = $user_info->url;
   $data["location"] = $user_info->location;
   $data["bio"] = $user_info->description;
}

So, in $data you will have the name, url, location and description of a user (you can retrieve more information, just look at the JSON file structure here: http://apiwiki.twitter.com/Twitter-REST-...hod:-users show)

in the end, you just need:

Code:
echo json_encode($data);
if you used JQuery to do the call

or

Code:
$this->load->view('some_view', $data);
if you want to pass the content to a view

Is this what you want?

Luis Gonçalves
#5

[eluser]clayko[/eluser]
Hey Luis,

This is a HUGE help... I only have one question. First: How do you store his id in the first place? Currently, the code only stores the oauth access tokens.

Thanks a lot,
Clay
#6

[eluser]luisfmgoncalves[/eluser]
Just a small trick...

If you notice, your access_token have the form: id-code, so where you save the access_token and access_token_secret I have the following code:

//save user ID in the session
Code:
list($id, $code) = explode("-",  $auth['access_token']);
$this->session->set_userdata('id', $id);

And there it is! Anytime you want to use the id, just do:

Code:
$id = $this->session->userdata('id');

**Edit**
Anyway, I think you can use the method:
Code:
$user_info = $this->twitter->call('users/show', array('id' => $id));
with the username intead of the id... Just try it!
**Edit**

Helps?

Luis Gonçalves
#7

[eluser]clayko[/eluser]
Hey again... I feel like I'm on the verge of a breakthrough thanks to you... Unfortunately, I can't get the code to work. Below is (part) of the twitter code...

I get the error: "Undefined property: stdClass::$name" when I try to load a view. I assume I'm making a simple mistake... do you know what it is?

Thanks a ton,
Clay

Code:
$this->load->library('session');

            $tokens['access_token'] = NULL;
            $tokens['access_token_secret'] = NULL;

            // GET THE ACCESS TOKENSin
            

            $oauth_tokens = $this->session->userdata('twitter_oauth_tokens');

            if ( $oauth_tokens !== FALSE ) $tokens = $oauth_tokens;

            $this->load->library('twitter');

            $auth = $this->twitter->oauth($consumer_key, $consumer_key_secret, $tokens['access_token'], $tokens['access_token_secret']);
            
    

            if ( isset($auth['access_token']) && isset($auth['access_token_secret']) )
            {
                // SAVE THE ACCESS TOKENS

                $this->session->set_userdata('twitter_oauth_tokens', $auth);
                
                        list($id, $code) = explode("-",  $auth['access_token']);
                        $this->session->set_userdata('id', $id);
                
                if ( isset($_GET['oauth_token']) )
                {
                    $uri = $_SERVER['REQUEST_URI'];
                    $parts = explode('?', $uri);

                    // Now we redirect the user since we've saved their stuff!

                    header('Location: '.$parts[0]);
                    return;
                }
            }

            // This is where  you can call a method.
            $id = $this->session->userdata('id');

            
            $user_info = $this->twitter->call('users/show', array('id' => $id));
            
        
               $data['name'] = $user_info->name;
#8

[eluser]luisfmgoncalves[/eluser]
Maybe you are using the name property in a wrong way...

If you have:
Code:
$data['name'] = $user_info->name;

in your view you will acees the name property like:
Code:
<span>&lt;?php echo $screen_name; ?&gt;</span>

You are using like this?

Another question... You afected this 2 variables:
Code:
$tokens['access_token'] = NULL;
$tokens['access_token_secret'] = NULL;

With the values that you have when you register the application, right?

Luis Gonçalves
#9

[eluser]clayko[/eluser]
I was just trying to echo the $data['name'] from within the controller... When I try to echo $screen_name from the view I get another unknown variable error.

Will you take a look at my code for me? I think there's something wrong with the getting of the ID in the first place.

I really appreciate all your help.

EDIT: Regarding your token question, yes, I have the correct token info from the twitter oauth registration.

Thanks again,
Clay
#10

[eluser]clayko[/eluser]
My controller code (most of it... not the code to load the view, etc)

Code:
$consumer_key = 'MY KEY';
            $consumer_key_secret = 'MY KEY'
            // For this example, we're going to get and save our access_token and access_token_secret
            // in session data, but you might want to use a database instead :)

            $this->load->library('session');

            $tokens['access_token'] = NULL;
            $tokens['access_token_secret'] = NULL;

            // GET THE ACCESS TOKENSin
            

            $oauth_tokens = $this->session->userdata('twitter_oauth_tokens');

            if ( $oauth_tokens !== FALSE ) $tokens = $oauth_tokens;

            $this->load->library('twitter');

            $auth = $this->twitter->oauth($consumer_key, $consumer_key_secret, $tokens['access_token'], $tokens['access_token_secret']);
            
    

            if ( isset($auth['access_token']) && isset($auth['access_token_secret']) )
            {
                // SAVE THE ACCESS TOKENS

                $this->session->set_userdata('twitter_oauth_tokens', $auth);
                
                        list($id, $code) = explode("-",  $auth['access_token']);
                        $this->session->set_userdata('id', $id);
                
                if ( isset($_GET['oauth_token']) )
                {
                    $uri = $_SERVER['REQUEST_URI'];
                    $parts = explode('?', $uri);

                    // Now we redirect the user since we've saved their stuff!

                    header('Location: '.$parts[0]);
                    return;
                }
            }

            // This is where  you can call a method.
            $id = $this->session->userdata('id');

            
            $user_info = $this->twitter->call('users/show', array('id' => $id));
            
        
               $data['name'] = $user_info->name;
            
            echo $data['name'];

THE ERROR:

Code:
Message: Undefined property: stdClass::$name
Filename: controllers/test.php
Line Number: 224

Line 224: $data['name'] = $user_info->name;




Theme © iAndrew 2016 - Forum software by © MyBB