Vladimir
Vladimir

Reputation: 63

How to decode twitter json with 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();


?>

Upvotes: 0

Views: 1649

Answers (3)

Albert Kozłowski
Albert Kozłowski

Reputation: 476

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'];
}

Upvotes: 2

YouSer
YouSer

Reputation: 393

Try:

json_decode($posts, TRUE) //it returns an associative array of values

Upvotes: 0

iwcoder
iwcoder

Reputation: 142

php function json_decode will do it

Upvotes: 0

Related Questions