我正在尝试使用带有symfony2和twig的Twitter API 1.1。到目前为止,我已成功将所有推文作为数组传递给模板,但我无法获取最新的推文。
除了数字或帖子之外,一切正常,所以我发布了完整的代码来帮助其他人。
推特功能
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;
}
控制器:
/**
* Index page
*
*/
public function indexAction()
{
$twitters = $this->twitter_latest();
return $this->render('mwwqTestBundle:Main:index.html.twig', array("twitters" => $twitters));
}
Twig模板:
<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] }}
无效
答案 0 :(得分:2)
如果您只想显示一条推文,为什么需要将多条推文传递给视图?
你可以这样做:
{% if loop.first %}
{{ twitter.text }}
{% endif %}
要更改控制器中的计数:
据推测,您只是想直接更改url变量。我认为你需要做类似以下的事情。当然,我已经在某个地方看到它,但找不到原作者:
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;
}
答案 1 :(得分:-1)
{% for twi in tweet|slice(0, 2)%}
<li>
<div>{{twi.text}}</div>
<small><strong>{{twi.created_at|date("m/d/Y")}}</strong></small>
</li>
{% endfor %}