Twitter API 1.1 error 215 upload on Altervista - php

I'm using the twitter API v1.1 and I'm trying to make a simple query on twitter (the list of followers of a user).
I've searched a lot on internet and I found many answers to this problem but I don't know what is my mistake.
I've look this thread on stack overflow Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
and this is the code I wrote on my php file and upload it on Altervista.
Of course on Altervista I have my TwitterAPIExchange.php file and the twitteroauth directory set properly. I try to run it but I got this message:
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
$consumer_key = "*****",
$consumer_secret = "*****",
$access_token = "*****",
$access_token_secret = "*****"
);
$url = 'https://api.twitter.com/1.1/followers/list.json';
$getfield = '?username=J7mbo&skip_status=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>

Related

How to fetch realtime tweets posted by particular twitter account / user?

Below code gives all tweets in array format for particular twitter account / user
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "your twitter account access_token",
'oauth_access_token_secret' => "your twitter access_token_secret",
'consumer_key' => "your twitter consumer_key",
'consumer_secret' => "your twitter consumer_secret"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = 'GET';
$getfield = '?q=+from:test_account';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") { echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
echo "<pre>";
print_r($string);
echo "</pre>";.
But I want to do following 2 things.
Whenever I click / open any twitter account page on my website, it should give real time tweets for that twitter account. Means whenever new tweet(s) for that account are posted, they should be displayed immediately on my website.
Currently I need to generate oAuth data (access token, access token secret, consumer key, consumer secret) for each twitter account. So is there any way that I can fetch tweets of particular twitter account and display them on my website without generating above 4 mentioned values?
Thank you,
First, you don't have to make new app for your user.
Second, the url you are using is just to access you timeline so that you can retrieve tweets from your account. Only for searching from user's tweets, you will need their screen name like #name.
try this,
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=from:test_account';
$twitter = new TwitterAPIExchange($settings);
$tweet = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($tweet,1);
As suggested in documents, here
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html

Concatenating twitter api get request while using variables

I have a lat and long saved like this,
$lat_value = $_REQUEST['latitude'];
$lon_value = $_REQUEST['longitude'];
When adding the variables to the $getfield String like this,
$getfield = '?q=test&geocode='.$lat_value.','.$lon_value.',1mi&count=100';
I keep getting this error,
"You must provide valid coordinates, IP address, query, or attributes." }
If it's a syntax error could someone please explain why this does not work to me.
Full request and Response code,
<?php
error_reporting(E_ALL);
$lat_value = $_REQUEST['latitude'];
$lon_value = $_REQUEST['longitude'];
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "XXX",
'oauth_access_token_secret' => "XXX",
'consumer_key' => "XXX",
'consumer_secret' => "XXX"
);
$url = 'https://api.twitter.com/1.1/geo/search.json';
$getfield = '?q=test&geocode='.$lat_value.','.$lon_value.',1mi&count=100';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
// Response from twitter
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
As per the documentation says, you have to specify either lat, long, ip or query, and you are not specifying any of them. I don't see any of those parameters in your querystring.
Try with this query:
$getfield = "query=test&lat={$lat_value}&long={$lon_value}&accuracy=1m&max_results=100"

Follower count number in Twitter

How to get my followers count number with PHP.
I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.
I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=google but is is showing an error(Bad Authentication data).
Here is my code:
$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
If do it without auth (replace 'stackoverflow' with your user)
$.ajax({
url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow"
dataType : 'jsonp',
crossDomain : true
}).done(function(data) {
console.log(data[0]['followers_count']);
});
with php
$tw_username = 'stackoverflow';
$data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username);
$parsed = json_decode($data,true);
$tw_followers = $parsed[0]['followers_count'];
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Use this instead:
<?php
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
Parsing the XML might be easier in some cases.
Here's a solution (tested):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
Hope this helps!

how to post tweet using twitter 1.1 api and twitteroauth

I use the below code to retrieve my tweets and echo json. This works fine.
<?php
session_start();
require_once('includes/twitter/twitteroauth.php');
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
$connection = new TwitterOAuth($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);
?>
Now i want to send a tweet using the similar code but it doesnot work. I am not sure if the sending syntax is correct. so please someone help me.
<?php
session_start();
require_once('includes/twitter/twitteroauth.php'); //Path to twitteroauth library
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
// start connection
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
//message
$message = "Hi how are you";
//send message
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => $message));
?>
I was using twitteroauth.php to post tweets myself when the new API broke it. You are using the $connection->post correctly, but it appears that function does not work anymore. The easiest solution I found was to swap out the twitteroauth.php with J7mbo's twitter-api-php file for the new 1.1 API:
https://github.com/J7mbo/twitter-api-php
Here's his step-by-step instructions for using it. I think you'll be pleasantly surprised to find you can leave most of your code the same, just switch the twitteroauth calls with his function calls at the appropriate places:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
He doesn't provide the specific example of posting a tweet, but here's what what you need for that function:
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Hi how are you' );
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
With the new twitter API, you won't need to provide your username/password. The authentication token will handle everything.
Just use the example:
$connection->post('statuses/update', array('status' =>$message));
Try it
you won't need to username/password you can post via API key read the tutorial here.
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
The issue is about enconding the value need to be enconde :
Example
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => rawurlencode($message)));
If you check in the library recomended https://github.com/J7mbo/twitter-api-php
That the way they encode the params

Twitter follower count number

Is the only way to get the follower count number in plain text is using cURL? or does the twitter API provides any such option?
https://api.twitter.com/1/users/lookup.json?screen_name=tvdw (my profile, just replace the screen name)
Also available as XML: https://api.twitter.com/1/users/lookup.xml?screen_name=tvdw
Obtaining it in PHP:
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=tvdw'), true);
echo $data[0]['followers_count'];
In API version 1.1 you can use: https://dev.twitter.com/docs/api/1.1/get/users/show
the 'followers_count' field should contain the follower count number.
In API version 1 which is deprecated you can use: https://dev.twitter.com/docs/api/1/get/users/show
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Use this instead:
<?php
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
Follow #twitterapi
<script>
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0];
if(!d.getElementById(id)){
js=d.createElement(s);
js.id=id;
js.src="//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);
}
}
(document,"script","twitter-wjs");
</script>
data-show-count = "true"

Categories