Help with Abraham's twitteroauth


#1

Hi,

I am working my way through Abraham’s twitteroauth lib for php and seem to be getting somewhere but then taking 2 steps backwards.

Here is my code as is:-

<html>
<head>
<title>Twitter Client example using PHP library - viralpatel.net</title>
    <style>
        body {
            font-family: sans-serif;
            font-size: 12px;
        }
        #update {
            height: 90px;
            width: 500px;
            background-color: #EFEFEF;
            margin-bottom: 5px;
            padding: 5 5 5 5;
        }
        #update #left {
            float:left;
            display: inline;
            padding-right: 7px;
        }
        #update .user {
            font-weight: bold;
            color: #0383FF;
			
        }
		
		A:link { text-decoration: none }
        A:active { text-decoration: none }
        A:visited { text-decoration: none }
    </style>
    
</head>
<body>
 
    <div id="updates">

<?php

/* Load required lib files. */
session_start();
require_once('twitteroauth.php'); 
require_once('config.php');

$twitterUser = $field_twitter_url;

    $connection = new TwitterOAuth (CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET); 
	
	$statuses = $connection->get('statuses/home_timeline', array('screen_name' => $twitterUser));

foreach ($statuses  as $status) {

?>
 
        <div id="update">
            <div id="left"><a href="https://twitter.com/#!/<?=$status->user->screen_name?>" target="_blank"/><img width="48px" height="48px" src="<?=$status->user->profile_image_url?>"/></a></div>
            <div id="right">
                <div class="user"><a href="https://twitter.com/#!/<?=$status->user->screen_name?>" target="_blank"/><?=$status->user->screen_name?></a>&nbsp;<?=$status->user->name?></div>
                <div class="detail">
                <?=$status->text?>
                </div>
            </div>
        </div>
<?php
}
?>
    </div>
 

</body>
</html>	

I know the code is pretty rough at the moment.

The issues I am trying to solve are how to add a max return count of 5 to this existing code?

While this code currently works another issue I have noticed is that when a retweet by someone I am following is returned, instead of showing the profile_image of the original tweeter it show the profile_image of the retweeter. Also in the text of the retweet it starts by saying:-
RT @ and then the original tweeters screen_name.

By starting the text like this on retweets it causes the text in most cases to extended beyond the 140 character limit and thus part of the text is cut off.

I really want retweets to appear as they do on my Twitter home page i.e. the actual profile_image of the original tweeter along with their actual tweet and then a small piece of text above the message to say ‘RT by’ screen_name.

Any help appreciated…,

Kind regards.


#2

To add a count parameter update your get like this:

$statuses = $connection->get('statuses/home_timeline', array('screen_name' => $twitterUser, 'count' => $count));

You will also probably want to add the include_rts=true parameter and var_dump the results to see the structure returned.


#3

Thank you Abraham, I will certainly have a look at that.

Regards…,


#4

I could see where you were coming from but I have found an alternative procedure to limit the responses to 5, I changed the code below in twitteroauth.php from:-

/** * Format and sign an OAuth / API request */ function oAuthRequest($url, $method, $parameters) { if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { $url = "{$this->host}{$url}.{$this->format}"; } $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); $request->sign_request($this->sha1_method, $this->consumer, $this->token); switch ($method) { case 'GET': return $this->http($request->to_url(), 'GET'); default: return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); } }

to

/** * Format and sign an OAuth / API request */ function oAuthRequest($url, $method, $parameters) { if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { $url = "{$this->host}{$url}.{$this->format}?count=5"; } $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); $request->sign_request($this->sha1_method, $this->consumer, $this->token); switch ($method) { case 'GET': return $this->http($request->to_url(), 'GET'); default: return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); } }

I guess there is probably a prettier way to do this.

Regards…,

MT


#5

Don’t edit twitteroauth.php. The prettier (and correct) way to do it is in my initial response. What happens when you need to make a request to a different timeline where you need six statuses?


#6

Tweet


#7

I am a noobie at this stuff and couldn’t quite get my head around your initial reponse, I can see that it is the correct way though.

I guess what is needed is a loop, yeah?

Would the loop wrap around, ($statuses as $status) ?

Do I need to set the $count variable as 5 for instance before the $connection statement in my original code example?

Maybe if you would be so kind as to point me in the right direction.

Much appreciated…,

MT


#8

Righty then, I solved that problem but still cannot figure out the RT issue.

What I am geeting is, for example:-

RT @Vince1905:

infront of the text output on every RT that appears in my home_timeline.

Also it shows the screen_name and profile_image of the retweeter.

On the actuall Twitter home page my timeline shows the profile_image & screen_name of the original tweeter and then obviously does not contain the RT @Vince1905: at the front of the tweet.

I am really trying to get my small widget to mimick Twitter as much as possible.

How do I display the original tweeter of a RT in my home_timeline?

Kind regards…,


#9

You either need to set $count = 5; or replace $count with 5 in the request.

$statuses = $connection->get('statuses/home_timeline', array('screen_name' => $twitterUser, 'count' => 5));

#10

You have to use the include_rts=true flag and implement it the same way as count.

$statuses = $connection->get('statuses/home_timeline', array('screen_name' => $twitterUser, 'count' => $count, 'include_rts' => true));

You then have to look at the response and you will find a retweeted_status object that contains what you are looking for.


#11

Thank you Abraham,

I have got it now with your help.

Here is a screenshot that show an RT. All I have left to do now is button down the CSS.

As a work in progress, my timeline is available here:-

http://www.madtogger.co.uk/twit/

Kind regards…,

MT


#12

I am trying to output the result into a file using PHP on the CLI, e.g.:

/usr/bin/php timeline.php $ck $cs $ak $as

timeline.php looks like this:

<?php $consumer_key = $argv[1]; $consumer_secret = $argv[2]; $access_key = $argv[3]; $access_secret = $argv[4]; //$file = $argv[5];

//$tweet = file_get_contents($file);

require_once(’/lib/twitteroauth/twitteroauth.php’);

$connection = new TwitterOAuth ($consumer_key ,$consumer_secret , $access_key , $access_secret );

$content = $connection->http(‘http://api.twitter.com/1/statuses/home_timeline.json’, ‘get’);
echo $content;
?>

The script seems to run through ok, but there is no output! How can I get @abraham’s OAuth libraries to output the result (to the screen or a file)?

Thanks!


#13

Well for one you are not using TwitterOAuth correctly. It may not be your issue but it is good to start off on the right foot.

$content = $connection->get('statuses/home_timeline');

If you echo ‘test output’; do you see test output printed to the console?


#14

Hello -

It all seems to work ok but now I cannot parse the output correctly, e.g.

<?php $consumer_key = $argv[1]; $consumer_secret = $argv[2]; $access_key = $argv[3]; $access_secret = $argv[4];

require_once(’/lib/twitteroauth/twitteroauth.php’);

$connection = new TwitterOAuth ($consumer_key ,$consumer_secret , $access_key , $access_secret );
//$connection->format =‘xml’;

$content = $connection->get(‘statuses/public_timeline’);
//$json_o = json_decode($connection->get(‘statuses/public_timeline’));
//$json_a=json_decode(print_r($content),true);
$json_o=json_decode(print_r($content));

//$connection->http(‘http://api.twitter.com/1/statuses/home_timeline.json’, ‘get’);
//echo “$content”;
//echo “test output”;
//print_r($content);

//foreach ($json_a[user] as $x) {
//echo ‘
//screen_name: ‘.$x[user][screen_name].’
//friends_count: ‘.$x[user][friends_count].’
//’
//;
//}

echo $json_o[0]->user->screen_name;
?>

I want to parse the output to extract (in this instance) “screen_name”, for example.

Any ideas?

Thank you!


#15

In Abraham’s twitteroauth the $content is already json decoded

you already have an array with objects

foreach ($content as $item) { echo "
" .$item->user->screen_name. "\r\n"; echo "
" .$item->text. "\r\n"; echo "
\r\n"; } or $x = count($content); for ($i=0; $i<$x; $i++) { echo "
" .$content[$i]->user->screen_name. "\r\n"; echo "
" .$content[$i]->text. "\r\n"; echo "
\r\n"; }

#16

hey i am using your twitteroauth library but i don’t no y not able to get the access token
$connection =new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET)
$token=$connection->getRequestToken(CALLBACKURL);

using these lines to make a connection but i am getting this error "Could not connect to Twitter. Refresh the page or try again later. "


#17

hey resolved the problem actually i just forget to set the callback url on the application setting screen on twitter


#18

Glad to hear you got it working.


#19

let me hear you first, where do live, your nationalilty, what do yuo work ,…etc


#20

0 down vote favorite
share [g+] share [fb] share [tw]

I am using Abraham"s TwitterOauth library to build an application that connects with Twitter and reads the timeline.

It would be great if Abraham Williams himself would answer my question but anyone that is familiar with the library is more than welcome to respond. I am using the whole TwitterOauth library including the files that get called in this order: redirect.php, callback.php, index.php.

It sort of works: the user is redirected to twitter log in to give rights to the app and I can get the tokens. The files redirect.php, callback.php, index.php are being called in this order. The problem is in index.php when I do a:

$content = $connection->get(‘account/verify_credentials’);

$content contains [error]=>could not authenticate you. I saw a question about this already saying that the cause for this error is overwriting the access token. Now I am using all the files as they are available from download with the library. I didn’t modify anything. Where in these files could be the overwriting of the access token?

I printed the final access token in callback.php and it showes the same token each time and my twitter user ID and my twitter user name wich means it sort of works. I don’t know where is the problem. I don’t modify anything in the abraham library files.

Thanks