简体   繁体   中英

Help with Twitter API with TwitterOAuth and PHP with object attributes

i have been doing some integration with twitter using php and this api: https://github.com/abraham/twitteroauth

I've managed to tweet, but im having some difficulties in understand and evaluate the return values.

For instance in the file callback.php if i want to post a tweeter i have this code:

$result = $connection->post(  
'statuses/update',  
array(  
    'status' => 'Tweet teste de API!',  
));

but how do i know what $result is returning me? i've tried to echo it and for it but it doesnt seem to work, any insights on that? I want to be able to see all the returning keys available.

I've tried this: foreach ($result as $key => $value) { echo "$key, $value <br />"; } foreach ($result as $key => $value) { echo "$key, $value <br />"; }

but it only prints two keys then a error appers that i cant convert stdclass. Any insight on how to see this or better api documentation?

Thank you

When posting with statuses/update , the returned $result will be a PHP class/object containing the created tweet, structured the same as status/show:id .

It looks like http://dev.twitter.com/doc/post/statuses/update fails to mention that the created tweet will be returned upon success. But if you read the bottom of http://dev.twitter.com/doc/get/statuses/show/:id , you can see all the different data you can access from $result .

You should also be able to see the object's entirety with var_dump($result) .

This example should hopefully work for you; it posts the tweet, then echos the created tweet's text and the user's screen name:

$result = $connection->post(
    'statuses/update',
    array('status' => 'Tweet teste de API!') // no comma needed here, by the way
);
if ($result->id) {
    // Tweet posted successfully, and $result contains the tweet data
    echo $result->text . '<br />Tweeted by @' . $result->user->screen_name;
} else {
    // Tweet failed
    echo 'Status failed to be updated.';
}

Similarly, pretty much any Twitter resource found on http://dev.twitter.com/doc can be accessed with $result = $connection->post() .

Just have the 1st parameter be a string of the desired resource (eg statuses/update or users/show ), and the 2nd parameter be an array of the resource's parameters, then you can access whatever the resource returns with $result .

The Twitter API is at your fingertips, and abraham's twitteroauth library makes it easy:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM