Now I load the tweets from twitter by using a username like this: (keys and tokens are filled in my code)
<?php
session_start();
require_once("twitteroauth/twitteroauth.php"); // PATH TO TWITTEROUTH LIBRARY
if(isset($_GET['twitteruser']) && !empty($_GET['twitteruser'])) {
$twitteruser = $_GET['twitteruser']; // USER
}
$notweets = 60; // NUMBER OF TWEETS
// OAUTH SETTINGS APPLICATION (https://dev.twitter.com/apps/)
$consumerkey = "";
$consumersecret = "";
$accesstoken = "";
$accesstokensecret = "";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
$data = json_encode($tweets);
?>
As you can see I load the tweets with a username and set the number of tweets.
Can this easily be changed to load tweets with a search term?
I've search on google but strangely I can't find much.
I've found this in the twitter API but that doesn't seem to work ..
https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4
source: https://dev.twitter.com/docs/api/1.1/get/search/tweets
(I changed the link to that link)
But I get this output with a var_dump:
string(319) "{"statuses":[],"search_metadata":{"completed_in":0.004,"max_id":2.50126199841e+17,"max_id_str":"250126199840518145","query":"%23freebandnames","refresh_url":"?since_id=250126199840518145&q=%23freebandnames&result_type=mixed&include_entities=1","count":4,"since_id":2.40126199841e+16,"since_id_str":"24012619984051000"}}".....
The Twitter search API only returns tweets from the past week or so. See more info here.
The Search API is not complete index of all Tweets, but instead an index of recent Tweets. At the moment that index includes between 6-9 days of Tweets.
Note that the search feature on Twitter's website does query historical tweets, it is just the API that does not support them.
Related
I'm trying to do something very simple, find a Twitter share count for a given url. But with this new API, it has been rather difficult.
I'm writing this in PHP and I'm using the abraham/twitteroauth API.
At first I was using a url like this...
$tweets = $twitter->get("https://api.twitter.com/1.1/search/tweets.json");
as is show in the documentation here https://dev.twitter.com/rest/reference/get/search/tweets
However that continually resulted in 404's. So I changed the structure of my code according to this post Twitter API returning Error: 34 (404)
But now all I'm getting is 401's. That's Twitters error code 32, "Could not authenticate you."
And yes I've checked and double checked all my tokens and secrets. I've also tried things like adding a value to the callback url field.
Below is my code...
case 'twitter':
$consumerKey = '###';
$consumerSecret = '###';
$oauthToken = '###';
$oauthTokenSecret = '###';
$twitter = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
$tweets = $twitter->get('search/tweets', ['q' => 'post_url_goes_here']);
$count = "I'll figure out how to sort this data later";
$response[$service] = $count ? $count : 0;
break;
Anyone else run into this problem?
Thanks!
I am trying to returning trending topics in the USA from the twitter API using TwitterAuth0 however I am getting, {"errors":[{"message":"Sorry, that page does not exist","code":34}]} as a result. My code is as following:
session_start();
require_once("twitteroauth-master/autoload.php"); //Path to twitteroauth library
use Abraham\TwitterOAuth\TwitterOAuth;
$id = "23424977";
$consumerkey = "XXX";
$consumersecret = "XXX";
$accesstoken = "XXX";
$accesstokensecret = "XXX";
function getToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=".$id);
echo json_encode($tweets);
My actual credentials are in the keys/secrets, currently my callback url on twitter apps is the page of the feed I am returning, is this correct? (So the acutal page to see the feed)
Any help on this error is appreciated.
Figured out the issue:
Get piece needed to be:
$tweets = $connection->get("trends/place", [ "id" => 23424977 ]);
instead of:
$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=".$id);
The title pretty much sums it up: I can't find any pages using Get Statuses with the Twitter API.
Here's my code.
<?php
session_start();
require "abraham/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$twitteruser = "IliaVED"; //User name I'm looking for
$id = "486654831"; //Corresponding id
$notweets = 30; //how many tweets you want to retrieve
$consumerkey = "xxx";
$consumersecret = "xxx";
$accesstoken = "xxx";
$accesstokensecret = "xxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
echo $tweets;
?>
I tried using the ID instead of the screen name and it still doesn't find it.
You can clearly see that the user exists:https://twitter.com/IliaVED
I tried with different users and it does the same thing..
This is the error I get:
{"errors":[{"message":"Sorry, that page does not exist","code":34}]}
You're missing = after screen_name in URL GET parameters part. Use:
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=".$notweets);
EDIT: Actually, you are using the library in wrong way, try with:
$tweets = $connection->get("statuses/user_timeline", ["screen_name" => $twitteruser, "count" => $notweets]);
Library itself has base URL and adds it to the path you provide and handles array of URL GET parameters.
I've been using this script to get tweets using twitter OAUTH on a local version of my website. When I upload it to my live web server it doesn't work.
It returns a JSON prettified list of twitter data based on a string that I provide.
Could anyone tell me why it isn't returning the JSON response on the live server?
<?php
session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 6;
$consumerkey = "**************************";
$consumersecret = "**************************";
$accesstoken = "**************************";
$accesstokensecret = "**************************";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$search_term = urlencode("pizza");
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=$search_term&count=$notweets&result_type=recent&include_rts=false");
header('Content-Type: application/json');
echo json_encode($tweets, JSON_PRETTY_PRINT);
?>
SOLVED
I updated my php.ini file to display errors and was given the following message.
Use of undefined constant JSON_PRETTY_PRINT
I was then able to solve my problem. Thanks to all who commented and I learnt how to track down errors appropriately in a live PHP / server environment.
I've been trying to get the latest status back from a user's twitter feed using Abraham Williams' Twitter Oauth Library (https://github.com/abraham/twitteroauth) I followed the tutorial found at http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/ and created the get_tweet.php file as my index. When I run this on my website an all white page, with "null" in the top left corner is displayed.
As far as I am to understand my Oauth keys I have are valid, I am using 000webhost.com to host my website, my webserver is using PHP 5.2.17 and cURL is enabled, From the tutorial and sample files I have been using my index file should be correct, my website can be found at http://authortryndaadair.site90.net where the results of this call is being dispayed.
I have been able to troubleshoot a small amount, but I am unsure of what else could try to get this Api call working. Any help in solving this problem would be much appreciated.
Below is the contents of the index file substituting for get_tweet1.1.php:
session_start();
// Path to twitteroauth library
require_once("twitteroauth/twitteroauth/twitteroauth.php");
$twitteruser = "JaneSmith";
$notweets = 10;
$consumerkey = "123456";
$consumersecret = "789123";
$accesstoken = "456789";
$accesstokensecret = "1023456";
function getConnectionWithAccessToken($cons_key, $cons_secret,
$oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret,
$oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret,
$accesstoken, $accesstokensecret);
$tweets = $connection->get(
"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="
. $twitteruser . "&count=" . $notweets
);
echo json_encode($tweets);
Assuming you are running the latest TwitterOAuth code the get line should look like this.
$tweets = $connection->get(
"statuses/user_timeline",
array("screen_name" => $twitteruser, "count" => $notweets)
);