The php code below should work however, I receive an error, empty response, from my server. Ive tried shutting down firewall but no luck. Can you please help me I have been trying different methods for ages I have no idea whats wrongs. Thank you.
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=cloud%20computing&count=5&result_type=popular';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>
you need to use json_decode : http://php.net/manual/en/function.json-decode.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=cloud%20computing&count=5&result_type=popular';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$string = json_decode($response, true); // there you handle twitter response
print_r($string); // here is the echo'd result, not needed after
// from here, you have access to the data
// then, a foreach loop will give you access to whatever you want to echo
foreach($string['statuses'] as $tweets) {
$tweet = $tweets['text'];
echo "<p>Tweet: $tweet </p>";
}
?>
updated with the loop example
Related
With this code, I can upload image and print Media ID but I can't post a tweet with image. Could you help me?
Thanks
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_include_path('/home/.....');
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "...",
'oauth_access_token_secret' => "...",
'consumer_key' => "...",
'consumer_secret' => "..."
);
$twitter = new TwitterAPIExchange($settings);
$file = file_get_contents('aaa.jpg');
$data = base64_encode($file);
// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
"media_data" => $data,
);
$json = $twitter
->buildOauth($url, $method)
->setPostfields($params)
->performRequest();
// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;
print_r($id);
////// Code is working to this line and can print media id like 213213214123123
$url = "https://api.twitter.com/1.1/statuses/update.json";
$twitter = new TwitterAPIExchange($settings);
$requestMethod = 'POST';
$response = $twitter->setPostfields(
array('status' => '', 'media_ids' => $id)
)
->buildOauth($url, $requestMethod)
->performRequest();
The media_id value can be used to create a Tweet with an attached photo using the statuses/update endpoint. Use your media_id as a parameter in the Statuses/update
I am trying to retrieve direct messages sent to my account real time. But the way, my script is working is that when someone sends me a message, I have to refresh the script to get the latest message. I would like to make it real time such that whenever someone sends me a message, I get it instantly. How do I go about it? Any help/suggestions is highly appreciated.
<?php
ini_set('display_errors', 1);
require_once('twitterapiexchange.php');
require_once("twitteroauth.php");
$settings = array(
'oauth_access_token' => "*********",
'oauth_access_token_secret' => "*********",
'consumer_key' => "*********",
'consumer_secret' => "*********");
$url = 'https://api.twitter.com/1.1/direct_messages.json';
$getfield = '?since_id=240136858829479935&count=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
foreach($string as $items)
{
$url = 'https://api.twitter.com/1.1/direct_messages/show.json?';
$requestMethod = 'GET';
$getfields = array('id' => $items['id']);
$twitter = new TwitterAPIExchange($settings);
$do = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
echo "<strong>Teet:</strong> ".$items['text']."<br />";
$senderId = $items['sender_id'];
$messageText = $items['text'];
$timeStamp = $items['created_at'];
$recipentId = $items['recipient_id'];
$messageId = $items['id'];
$screenname=$items['sender_screen_name'];
if(!empty($messageText)) {
// $data = file_get_contents('https://fb.craftsilicon.com/testphpbot/core.php??username=".$senderId."&tweetid=".$messageId."&tweettext=".$messageText."&tweettime="$timeStamp."&source=facebook&receiver=".$recipentId."');
$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452');
$obj = json_decode($json);
//var_dump($obj->results[0]->formatted_address);
$addr = $obj->results[0]->formatted_address;
$answer = "You said ".$messageText . " " .$addr;
echo "<strong>Answer:</strong> ".$answer."<br />";
$api_key='*********' ;
$api_secret= '*********' ;
$access_token = '*********';
$access_token_key='*********' ;
$connection = new TwitterOAuth($api_key,$api_secret, $access_token, $access_token_key);
$connection->post('direct_messages/new', array('user_id' => $senderId, 'text' => $messageText));
}
//var_dump(json_encode($items, true));
}?>
You'd need to use the streaming API and maintain a continuous connection to a user stream, then filter out direct messages from the responses.
So i am trying to get that tweet id of a retweeted id and since twitter recently allows you to retweet your own tweet, when i try to get the retweeted tweet's id of my own tweet, it gives me two variables when i use var_dump i want only one of it how do i extract?
i used
var_dump(tweet->id);
in the code and when i execute it, it shows like int(12381239) int(29384121)
i want to take the first int variable that is 12381239 and store it in a variable, i tried all i can but failed can anyone help me?
UPDATE : Here is the code
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xxxxxx-asdsaldjasd",
'oauth_access_token_secret' => "xxxxxx",
'consumer_key' => "xxxxxxx",
'consumer_secret' => "xxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$getfield = '?screen_name=username';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response);
foreach($tweets as $tweet)
{
if ($tweet->retweeted) // here is the part where i did var_dump
{
$url = "https://api.twitter.com/1.1/statuses/destroy/$tweet->id.json";
$requestMethod = 'POST';
$postfields = array(
'id' => '$tweet->id'
);
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
$response;
}
}
what i'm trying to do is undo the retweets, which was working fine until twitter let the accs rt themselves.
Could someone help me finish script, its taking posts from my twitter page, I need now to make it take only "text": "" .
Here is json:
http://142.4.211.155/~bingsw/tw/
And here is code where I get json:
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=MiDizajn&count=5';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>
If you want to get only text of the tweets try this:
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response,true);
foreach ($tweets as $tweet) {
echo $tweet['text'];
}
php function json_decode will do it
Try:
json_decode($posts, TRUE) //it returns an associative array of values
I am using twitter-api-php-master to search keywords through twitter but got Null return. Really not sure why it is like that.
My code is like:
<?php
#### Include the class file ####
require_once('TwitterAPIExchange.php');
#### Set access tokens ####
$settings = array(
'oauth_access_token' => "[something]",
'oauth_access_token_secret' => "[something]",
'consumer_key' => "[something]",
'consumer_secret' => "[something]"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=test&geocode=37.781157,-122.398720,1mi&count=100';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>
I pretty much copy this from example from https://github.com/J7mbo/twitter-api-php/wiki/Twitter-API-PHP-Wiki. But the result is just Null. Someone knows what is the problem?