Radolino Radolino -4 years ago 157
Twig Question

display latest tweet using api 1.1 in twig and symfony2

I am trying to use Twitter API 1.1 with symfony2 and twig. So far I have succeed to pass all tweets as an array to the template but I am unable to get the latest tweet ONLY.

Everything works ok except the number or posts, so I am posting full code to help others also.

twitter functions

public 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));
}

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

public function twitter_latest(){
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "***";
$oauth_access_token_secret = "***";
$consumer_key = "***";
$consumer_secret = "***";

$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 = $this->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;

// Make Requests
$header = array($this->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);

return $twitter_data;
}


Controller:

/**
* Index page
*
*/
public function indexAction()
{
$twitters = $this->twitter_latest();
return $this->render('mwwqTestBundle:Main:index.html.twig', array("twitters" => $twitters));

}


Twig template:

<div id="mainTwitter">
<ul id="twitter_update_list">
{% for twitter in twitters %}
{{ twitter.text }}
{% endfor %}
</ul>
</div>


{{ twitter.text[0] }}, {{ twitter[0].text }}, {{ twitter.text.[0] }}
are not working

Answer Source

Why do you need to pass more than one tweet to the view if you only want to display one?

You could do something like this:

{% if loop.first %}
    {{ twitter.text }}
{% endif %}

To change the count in the controller:

Presumably you were just trying to change the url variable directly. I think you need to do something like the following. Sure I've seen it somewhere but can't find the original author:

public function twitter_latest($count = 1){
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "***";
$oauth_access_token_secret = "***";
$consumer_key = "***";
$consumer_secret = "***";

$oauth = array( 'count' => $count,
                '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 = $this->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;

// Make Requests
$header = array($this->buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url . '&count=' . $count,
                  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);

return $twitter_data;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download