0

First of all I've never used JSON before. As you might know twitter has changed their api to v1.1 and i am trying to show count of followers on the page. I've followed this post and now able to see all of the json values

this is the code i am using

$token = 'CODE';
$token_secret = 'CODE';
$consumer_key = 'CODE';
$consumer_secret = 'CODE';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters
    'screen_name' => 'username',
    'count' => '2'
);

$oauth = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_token' => $token,
    'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
    'oauth_timestamp' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0'
);

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)

// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "https://$host$path";

// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it

// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

This is the minimised output

 [{"created_at":"Thu Mar 21 12:11:51 +0000 2013", .... 
"id":314710946549465088,"followers_count": "3" }]

and my question is how can i GET the followers_count value from this output and echo in a div.

Community
  • 1
  • 1
Emre
  • 23
  • 1
  • 8
  • FWIW, your question got few views because you didn't tag it with a language. Always tag a question with the language you use, if possible. You can answer you question yourself btw (better than editing the question). – Felix Kling Jun 17 '13 at 14:14
  • I was really stressed because of this issue and did make little mistakes :) thanks for the info ... – Emre Jun 17 '13 at 14:30

1 Answers1

0

Found the answer

Change this

$result = json_decode($json);

to

$result = json_decode($json, true);

Then add

 echo $result[0][user][followers_count];
Emre
  • 23
  • 1
  • 8