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.
Related
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.
I'm trying out the ActiveCollab API for my first time. I had to use StackOveflow to figure out how to get the API token since the docs don't tell me this.
Below is my code:
/* GET INTENT */
$url = 'https://my.activecollab.com/api/v1/external/login';
$fields = array(
'email' => "email#email.com",
'password' => "****"
);
$intent = curl_post_connector($url, $fields);
$intent = $intent->user->intent;
/* GET TOKEN */
$url = 'https://app.activecollab.com/my_app_id/api/v1/issue-token-intent';
$fields = array(
'intent' => $intent,
'client_name' => 'My App Name',
'client_vendor' => 'My Company Name'
);
$token = curl_post_connector($url, $fields);
$token = $token->token;
Everything above works and get's the token properly. What I find really weird is that I have to use API v1 to get this, and the docs on ActiveCollab's site don't mention any URL for API v5. It seems like this is the approach everything is taking here on StackOverflow.
Now with the token, I try to get my list of projects:
/* GET PROJECT */
$url = 'https://app.activecollab.com/my_app_id/api/v1/users';
$headers = array (
"X-Angie-AuthApiToken" => $token
);
$projects = curl_get_connector($url, $headers);
var_dump($projects);
But this does not work. There is no error returned - it instead returns an array of languages for some reason! I don't want to paste the massive json object here, so instead I'll link you to a photo of it: https://www.screencast.com/t/7p5JuFB4Gu
UPDATE:
When attempting to use the SDK, it works up until I try getting the token (which is just as far as I got without the SDK). I'm getting Server Error 500, and when looking at the logs, it says:
/home/working/public_html/ac/index.php(21): ActiveCollab\SDK\Authenticator\Cloud->issueToken(123456789)
#1 {main}
thrown in /home/working/public_html/ac/SDK/Authenticator/Cloud.php on line 115
This is line 115 of Cloud.php:
throw new InvalidArgumentException("Account #{$account_id} not loaded");
I honestly don't think I did anything wrong... there must be something wrong with my account ID.
Just for kicks, I commented out that line, and the error disappears and the page loads fine - except now I have no token...
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
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/