-1

I'm trying to get my latest tweets to display on my webpage and found and am using this peice of code.

            <?php
            function buildBaseString($baseURI, $method, $params) {
                $r = array();
            ksort($params);
            foreach($params as $key=>$value){
                $r[] = "$key=" . rawurlencode($value);
            }
            return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
            }

            function buildAuthorizationHeader($oauth) {
            $r = 'Authorization: OAuth ';
            $values = array();
            foreach($oauth as $key=>$value)
                $values[] = "$key=\"" . rawurlencode($value) . "\"";
            $r .= implode(', ', $values);
            return $r;
            }

            $url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk";
            $oauth_access_token = "SECUIRTY";
            $oauth_access_token_secret = "SECURITY";
            $consumer_key = "SECURITY";
            $consumer_secret = "SECURITY";

            $oauth = array( 'oauth_consumer_key' => $consumer_key,
                        'oauth_nonce' => time(),
                        'oauth_signature_method' => 'HMAC-SHA1',
                        'oauth_token' => $oauth_access_token,
                        'oauth_timestamp' => time(),
                        'oauth_version' => '1.0');



            $base_info = buildBaseString($url, 'GET', $oauth);
            $composite_key = rawurlencode($consumer_secret) . '&' .     rawurlencode($oauth_access_token_secret);
            $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
            $oauth['oauth_signature'] = $oauth_signature;



            $header = array(buildAuthorizationHeader($oauth), 'Expect:');
            $options = array( CURLOPT_HTTPHEADER => $header,
                          //CURLOPT_POSTFIELDS => $postfields,
                          CURLOPT_HEADER => false,
                          CURLOPT_URL => $url,
                          CURLOPT_RETURNTRANSFER => true,
                          CURLOPT_SSL_VERIFYPEER => false);



            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);
            $twitter_data = json_decode($json);
?>

Problem is I can't seem to figure out how to actually print the data once received.

j0k
  • 22,600
  • 28
  • 79
  • 90
Flatlyn
  • 2,040
  • 6
  • 40
  • 69

1 Answers1

2

You have two options on how to print the data. 1.) One is to use $twitter_data as a class (which is what you are currently doing). 2.) One is to use $twitter_data as an array. This is done by using:

 $twitter_data = json_decode($json,true);

instead of:

 $twitter_data = json_decode($json);

if you use $twitter_data as an array you can just go through each element by using:

foreach ($twitter_data as $elem)
{
  print_r($elem);
  echo '<br>';
}

If you use it as a class you would either have to know how each "element" is named and thus use echo $twitter_data->elementName; or use a reflexionclass to get a list of all "elements" within $twitter_data and parse through them.

Thomas
  • 2,886
  • 3
  • 34
  • 78