2013-07-04 67 views
2

我正在嘗試使用Twitter API 1.1與symfony2和樹枝。到目前爲止,我已經成功地將所有推文作爲數組傳遞給模板,但我無法僅獲取最新的推文。使用api 1.1顯示twig和symfony2中的最新tweet

一切工作正常,除了數量或職位,所以我張貼完整的代碼來幫助別人也。

Twitter的功能

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

} 

嫩枝模板:

<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] }}不工作

回答

2

爲什麼你需要通過MO如果你只想顯示一個視圖,那麼比視圖重複一次?

你可以做這樣的事情:

{% 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; 
} 
+0

如果我想知道怎麼樣,我就已經做到了!如何顯示第5條推文? – Radolino

+0

如果你只想要返回一個,你可以修改它,如https://api.twitter.com/1.1/statuses/user_timeline.json?count=1 - 我想不出一種方法來獲得特定的號碼回來。我會盡快回復你。 –

+0

沒有測試過,但是像這樣:{%if loop.index == 5%} should be working。 –

-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 %} 
+1

儘管此代碼片段可能會解決該問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan