I'm wondering if I can show the latest Tweet of multiple Twitter users specified by me.
This is what I have with the keys removed, they are indeed defined in my actual code:
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?user_id=McDonalds,Wendys,Dominos&count=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
echo '<pre>';
$response = var_dump(json_decode($response));
But it only outputs my latest Tweet. How can I make it so it outputs the latest Tweet of the specified users?
There are two ways to do this - neither is quite as simple as you want.
Firstly, you could request the user timeline for each user individually.
So call https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=McDonalds and then https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=Wendys and so on.
That might be slow. So, alternatively, you could create a list with all the users you want to follow.
Then you can use the List API to get all the recent tweets of those users.
Related
I have been testing this script and was able to get it to work to replying to tweets that I post on my own timeline, but I am trying to get it to work with Twitter mentions. All the data is passing correctly like the in_reply_to_status_id that I get from that mention, but it seems to not work.
The only other POST that I can see that "might" be what I need is POST direct_messages/events/new (message_create) but I don't want to send a private message, I want to reply to that mention.
I am suspecting that I cannot reply to a mention like I could to my own tweets.
Is there a way to reply to a mention and how do I do it or what am I doing wrong?
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => $twitter_accesstoken,
'oauth_access_token_secret' => $twitter_accesstokensecret,
'consumer_key' => $twitter_apikey,
'consumer_secret' => $twitter_apisecret
);
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$postfields = array(
'in_reply_to_status_id' => $in_reply_to_status_id,
'status' => $status
);
$requestMethod = 'POST';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setPostfields($postfields)
->buildOauth($url, $requestMethod)
->performRequest();
}
Solved
I figured it out. My code was fine I just needed to add "#username " . $status to status and it worked.
Also something else I learned that I think is worth mentioning to help others is it is important to look at the response at the in_reply_to_status_id_str field when returned. If it is blank then it is a new tweet, if it is not blank like 123456789123456789 That ID is the ID of the original tweet so you can relate the difference between the tweet and the reply to that tweet.
I'm using the Twitter Search API v1.1 to simply search by hashtag #monstercareers. This bit of code works for other hashtags, but will not work for this particular one. I know this API is slightly different from the one Twitter actually uses (which makes it difficult to test), but if you search #monstercareers on Twitter.com it works fine.
<?php
require_once('lib/TwitterAPIExchange.php');
$url = "https://api.twitter.com/1.1/search/tweets.json";
$getfield = "?q=#monstercareers";
$requestMethod = "GET";
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
?>
Here is the empty response I get with the above code:
{
statuses: [ ],
search_metadata: {
completed_in: 0.006,
max_id: 396701092337053700,
max_id_str: "396701092337053696",
query: "%23monstercareers",
refresh_url: "?since_id=396701092337053696&q=%23monstercareers&include_entities=1",
count: 15,
since_id: 0,
since_id_str: "0"
}
}
Does anyone have any idea on why this might be an inconsistency, and if there's a better way around this?
Doing some research I have found that the twitter API only looks back up to 6-9 days of tweets, so in the case you might want to look back on time, you need to use the until param.
Checkout the documentation on https://dev.twitter.com/docs/using-search on the Best Practices Section.
I am using the tmOAuth library.
With the new 1.1 API, the following is returning an error code 400 - but authentication was done (same authentication for statuses works)! The library I am using works fine for all calls, except this one!
$tmhOAuth->request(
'POST', $tmhOAuth->url('https://api.twitter.com/1.1/statuses/destroy/MYIDHERE.json'),
array(
'id' => MYIDHERE
)
);
The twitter API documentation states that you don't have to send the id in post - but this doesn't make any difference.
I have tested this today with two different libraries, and neither work.
Any suggestions - does anyone know if there is an issue with it??
According to your comment, you have tested this in two libraries for the 1.1 API.
You haven't tested it in this one though. Instructions here, although you seem to already have your credentials in hand.
This basically proves that the library you are using has the issue, not the twitter API. So either submit a bug report on github (how else are they to know?), or use another library like the one above.
The exact code required using the above library (and it works, I just tested it):
// Require the library file
require_once('TwitterAPIExchange.php');
// Set up your credentials
$settings = array(
'oauth_access_token' => "YOUR_TOKEN",
'oauth_access_token_secret' => "YOUR_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
// Put the correct ID in the URL
$url = 'https://api.twitter.com/1.1/statuses/destroy/YOURIDHERE.json';
// Set the request type
$requestMethod = 'POST';
// Set the post fields
$postfields = array('id' => 'YOURIDHERE');
// Make the request
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
// Dump the response
$result = json_decode($json);
var_dump($result);
If you're using the twitteroauth php library from Abraham Williams and trying to delete old tweets/retweets you need to construct the post() query as such:
$response = $connection->post('statuses/destroy/'.$tweetID, array()); //Curl url output = statuses/destroy/$tweetID.json
Greetings
Here's what's up:
I'm working on an app where the user or a page they administrate is tagged in their own image, however, tagging a page they administrate doesn't function, I have no trouble with the user being tagged.
Here's some code:
$tdata = array('tag_uid' => $fb_id,'x' => 0,'y' => 0);
$datatags[] = $tdata;
$attachment = array(
'access_token' => $access_token,
'tags' => $datatags
);
$attachment['image'] = '#'.realpath($image_name.);
$result = $facebook->api('/'.$album_id.'/photos', 'POST', $attachment);
$fb_id is either the ID for the user or the page. which is grabbed using /me/accounts in the Graph API
Thanks!
Per the documentation you cannot do this:
You can specify which user to tag using two methods: in the URL path
as PHOTO_ID/tags/USER_ID, or in a URL parameter as
PHOTO_ID/tags?to=USER_ID. Currently, you cannot tag a Page in a photo
using this API.
https://developers.facebook.com/docs/reference/api/photo/
I am trying to show the latest tweets on a webpage, but I am unsure of how to do it as I am very new to the twitter api, but here is what I have thus far.
function my_streaming_callback($data, $length, $metrics) {
echo $data;
}
require '../tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'key',
'consumer_secret' => 'secret',
'user_token' => 'token',
'user_secret' => 'secret',
));
$method = 'http://stream.twitter.com/1/statuses//show/:id.json';
//not sure where I am supposed to get the :id from?
$params = array(
//not sure what to put here, I would like to display the last 5 tweets
);
$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
$tmhOAuth->pr($tmhOAuth);
I am using this https://github.com/themattharris/tmhOAuth to authenticate, and then interface with the twitter api, but I am finding it very confusing as all of this is very new to me.
So basically I would just like to try and get the latest tweets, any help would be GREATLY appreciated, as I need to get this done ASAP, thanx in advance! :)
Stream API returns you only the tweets posted after you connected. To get previous tweets you need to use general REST API method: http://dev.twitter.com/doc/get/statuses/show/:id
$tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/show/$id'));
$tmhOAuth->pr(json_decode($tmhOAuth->response['response']));
where $id is the user's id.
If you wish you can use THIS beautiful jQuery plugin. It do the some thing as you require. For more information visit http://tweet.seaofclouds.com/