How to decode twitter json with php? - php

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

Related

Get hashtag posts with specific user

I want to get tweets with a specific hashtag with a specific user. I used
this request to that. But it's not working. But its working without but in the documentation, they said this is working.
<?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' => ""
);
/** Note: Set the GET field BEFORE calling buildOauth(); * */
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$hashtag = 'zkst';
$getfield = '?q=from%3Ajulia44K%20%23' . $hashtag . '&result_type=mixed&include_entities=true&count=20';
// Perform the request
$twitter = new TwitterAPIExchange($settings);
$json_output = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
// Let's calculate how many results we got
$json = json_decode($json_output);
$n = count($json->statuses);
echo $n;
?>

Php Twitter Post tweet with media

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

PHP error empty response

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

var_dump(tweet->id); gives int(198231293) int(123981623) how to i retrieve one and store it on a variable?

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.

Applying Twitter API using example but return Null

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?

Categories