I am trying to use the TwitterOAuth solution from Abraham.
I've done everything as described in his documentation, but I still get that error:
{"errors":[{"code":32,"message":"Could not authenticate you."}]}
That's my source code:
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerkey = 'xx';
$consumersecret = 'xx';
$accesstoken = 'xx';
$accesstokensecret = 'xx';
$connection = new TwitterOAuth($consumerkey,$consumersecret,$accesstoken,$accesstokensecret);
$tweets = $connection->get("search/tweets.json?q=superbowl");
echo json_encode($tweets);
All the keys are correct. I have no clue, why this still happens.
The App Permission are Read only.
What I want to do is get tweets based on a search query.
Do you have any idea how to fix this? Let me know, if you need some more information.
Hope this will fix your problem....
<form action='' method='get' ><input type='text' name='q'> <button type='submit' >click</button></form>
<?php
require "autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = "youconsumerkey";
$consumerSecret = "yourconsumersecretkey";
$oauthToken = "youoauthtoken";
$oauthTokenSecret = "youroauthtokensecret";
$twitter = new TwitterOAuth($consumerKey,$consumerSecret,$oauthToken,$oauthTokenSecret);
$search = isset($_GET['q']);
if(isset($_GET['q'])){
$tweets = $twitter->get('search/tweets',array('q'=>'%23'.$_GET['q'],'result_type'=>'recent','count'=>'10'));
//var_dump($tweets);
foreach($tweets->statuses as $value){
echo $value->user->name; echo "<br/>";
//echo json_encode($tweets);
}
}
?>
#
Actually in my case the reason behind ]Could not authenticate you; Error: 32]
was this line of code that i had tested for my project:
// $tweets = $twitter->get('search/tweets.json?q=mytest&result_type=recent&count=10');
I am Using TwitterOAuth PHP Library for the Twitter REST API, https://twitteroauth.com/ .
HTTP
GET https://api.twitter.com/1.1/search/tweets.json
TwitterOAuth
$tweets= $twitter->get("search/tweets.json", array("result_type" => 'recent', "count" => 10)); //should be like this
// $tweets= $twitter->get('search/tweets.json?q=mi_twitter_test&result_type=recent&count=10'); //throws authentication error
Related
I'm attempting to use Twitter's API, which to me seems hugely overcomplicated. I want to simply get a list of tweets that include a hashtag ('#test' for now), along with the username.
This is what I have so far using Abraham's TwitterOAuth:
<?php
$token = 'MY_TOKEN';
$token_secret = 'MY_TOKEN_SECRET';
$consumer_key = 'MY_CONSUMER_KEY';
$consumer_secret = 'MY_CONSUMER_SECRET';
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth( $consumer_key, $consumer_secret, $token, $token_secret );
$connection->host = "https://api.twitter.com/1.1/";
$connection->ssl_verifypeer = TRUE;
$connection->content_type = 'application/x-www-form-urlencoded';
$tweets = $connection->get('https://api.twitter.com/1.1/search/tweets.json?q=%23test&result_type=recent');
echo $tweets;
?>
This produces the error Object of class stdClass could not be converted to string in the echo $tweets; line.
How do I go about fixing this?
EDIT:
I have changed the code to $tweets = $connection->get('search/tweets', ["q" => "#test", "result_type" => "recent"]); and var_dump($tweets); which seems to work. Now I just have to figure out how to parse this.
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 am working on an app to show the tweets done in 1 mile radius of given geolocation (Latitude and Longitude). This is my PHP code,
<?php
// $lat = $_GET['lat'];
$lat = 26.511740;
// $long = $_GET['long'];
$long = 80.234973;
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 100;
$consumerkey = "XXXX";
$consumersecret = "XXXX";
$accesstoken = "XXXX-XXXX";
$accesstokensecret = "XXXX";
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/search/tweets.json?geocode=".$lat.",".$long.",5mi&result_type=recent&count=".$notweets);
// echo $tweets;
echo json_encode($tweets);
?>
I am using Wamp server (PHP V5.5.12) and my code is working fine on it. But when I host my application on some free hosting sites (I have tried hostinger.in and 000webhost.com), this script fails and just prints 'null'.
Please help me to solve this problem.
Thanks in advance.
I've tried in both, hostinger and 000webhost and several others. The reason they dont work is the library to connect to twitter uses php curl, and many free hosting have disabled curl, or outgoing connections or twitter refuses the curl connection when it comes from the ips from free hosting servers. For what I've read in internet it's probably because many hackers have been messing with twitter and hosting from free hosting accounts. So finding a free hosting with cpanel that works with Twitter API it's a challenge, i've tried over 20 and they dont work, some of them automatically deleted the account or the file or block the ftp access if you try to curl to twitter
Likely to be related to the libraries available to you.
Add error_reporting(E_ALL) to the top of the script.
Check that cURL is installed on the cheap hosting as I believe that's the only php library that twitteroauth requires.
What is your TwitterOAuth version? Your code seems to be so legacy and not compatible with latest version.
https://twitteroauth.com/
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
$to = new TwitterOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
if (isset($tweets->errors[0]->message)) {
echo 'Error: ' . $tweets->errors[0]->message;
} elseif (!is_array($tweets)) {
echo 'Unknown Error';
} else {
echo '<pre>';
var_dump($tweets);
echo '</pre>';
}
Alternatively, you can use TwistOAuth instead of TwitterOAuth. I'm the author of this library. This library is almost compabible with TwitterOAuth, but support strict exception handling. Error reasons are always to be clear.
https://github.com/mpyw/TwistOAuth
<?php
require 'TwistOAuth.phar'; // Or 'vendor/autoload.php' for composer
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
try {
$to = new TwistOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
echo '<pre>';
var_dump($tweets);
echo '</pre>';
} catch (TwistException $e) {
echo 'Error: ' . $e->getMessage();
}
Which code do you like?
I want to follow/unfollow some friends who authorize my Twitter app.
For example:
User signs in with my twitter application and he wants to follow some people
How does it work? I wrote some code here but not working, The session works fine, The user signs in but create/friendship not working, why?
<?php
session_start();
require_once('TwitterAPIExchange.php');
require_once('tmhOAuth.php');
require_once('tmhUtilities.php');
require_once('twitteroauth.php');
require 'twconfig.php';
echo $_SESSION['oauth_token'];
echo "<br />";
echo $_SESSION['oauth_token_secret'];
$twitteroauth = new TwitterOAuth($consumerKey, $consumerKeySecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] );
$twitteroauth->post('friendships/create', array('screen_name' => 'savanpaun'));
?>
Simply put, I want people follow/unfollow friends using signup in my application directly.
here is a sample code to follow someone.I’ve had used Abraham library you can get it from here
https://github.com/abraham/twitteroauth.
also this is a twitter documentation you can check it out
https://dev.twitter.com/rest/reference/post/friendships/create. and to unfollow someone just use 'friendships/destroy'
<?php
echo "<pre>";
$consumerKey = 'your consumer key';
$consumerSecret = 'your consumer secret key';
$oAuthToken = 'your oauth token';
$oAuthSecret = 'your oauth secret';
require_once('twitteroauth.php');
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//get friend list
$list= $tweet->post('friendships/create', array('screen_name' => 'archish9'));
var_dump(json_decode($list));
print_r($list);
?>
I copy paste you example and replace in the last line with destroy command
<?php
session_start();
require_once('TwitterAPIExchange.php');
require_once('tmhOAuth.php');
require_once('tmhUtilities.php');
require_once('twitteroauth.php');
require 'twconfig.php';
echo $_SESSION['oauth_token'];
echo "<br />";
echo $_SESSION['oauth_token_secret'];
$twitteroauth = new TwitterOAuth($consumerKey, $consumerKeySecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] );
$twitteroauth->post('friendships/destroy', array('user_id' => 'iduser'));
?>
this line
$twitteroauth->post('friendships/destroy', array('user_id' => 'iduser'));
I hope that this help you in some way
best
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