2013-06-11 77 views
27

我對於通常使用Twitter並且從未在任何項目中嵌入「最新推文」是全新的。我只是試圖在站點頁腳中嵌入3-4個最新的推文,而沒有其他功能。我一直在研究如何做到這一點,現在已經有一段時間了,並且遇到了一些麻煩。設置Twitter API,獲取最後幾條推文

我將下面的代碼片段添加到了項目中,工作得很好,但是,我不確定如何更新代碼段,以便使用我的Twitter帳戶而不是使用其設置的代碼片段。

<div id="twitter_update_list"> 
    </div> 
    <script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stackoverflow&include_rts=true&count=4&callback=twitterCallback2"> 
    </script> 

另外,我一直在閱讀中瞭解到最常用Twitter的API將停止因爲Twitter希望人們用自己工作不久,而不是第三方。

我不知道如何從這裏開始。我非常感謝在這方面的任何建議。回顧一下,我所要做的就是從我的賬戶中獲取最新的3-4條推文。

非常感謝提前!

+0

如果您需要一個javascript-only解決方案,您可以使用Jason Mayes的Twitter-Post-Fetcher http://jasonmayes.com/ projects/twitterApi /幾天後,我會嘗試新工作,今天它似乎是ag對於誰不能在服務器端工作的方式。 –

回答

59

所以你真的不想再做這個客戶端了。 (只是通過大量文檔就去了,開發者建議做所有的OAuth服務器端)

你需要做什麼:

首先:註冊於https://dev.twitter.com,並作出新的應用程序。

:注意:使用訪問令牌沿着你的消費鍵/祕密/機密

:下載Twitter的OAuth的庫(在這種情況下,我使用的PHP庫https://github.com/abraham/twitteroauth,額外的庫位於:https://dev.twitter.com/docs/twitter-libraries

:(如果使用PHP)確保捲曲功能時,若你對一個燈工作在這裏您需要的命令:

sudo apt-get install php5-curl 

第五:創建一個新的PHP文件,並插入以下內容:感謝湯姆·埃利奧特http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/

<?php 
session_start(); 
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library you downloaded in step 3 

$twitteruser = "twitterusername"; //user name you want to reference 
$notweets = 30; //how many tweets you want to retrieve 
$consumerkey = "12345"; //Noted keys from step 2 
$consumersecret = "123456789"; //Noted keys from step 2 
$accesstoken = "123456789"; //Noted keys from step 2 
$accesstokensecret = "12345"; //Noted keys from step 2 

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { 
    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
    return $connection; 
} 

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); 

echo json_encode($tweets); 
echo $tweets; //testing remove for production 
?> 

,你就大功告成了。 我知道這不是一個純粹的js解決方案,但通過閱讀新的Twitter API 1.1文檔,他們真的不希望你做這個客戶端網站。希望這個幫助!

+0

感謝您的回覆!這比獲得更多的工作要得到3條推文,但我必須做我應該做的事情;)我會試試看並回來看看。 – AnchovyLegend

+0

當你說「註冊dev.twitter.com」時 - 你是否需要這樣做,你想從(我的客戶的acc)或任何帳戶(即我自己的帳戶)獲取推文的帳戶? – NickG

+0

@NickG不,它只是一個Twitter帳戶。這是在上面的第五步中的鏈接解釋 – Mac

-14

其實twitter有許多限制,因爲像耐克和其他公司有很多比賽。推文的閱讀是有限的,因爲如果你正在閱讀最新的API,它實際上會稍微落後一點。

他們也控制了DM的延遲,這意味着即使你做不到,也不能即時DM,對方只會在X時間後纔會收到。如果你通過腳本來完成,並且即使你嘗試從一個單獨的IP地址發送很多DM,Twitter也會阻止你。

22

如何獲得用戶的最後幾鳴叫與PHP核心功能只(不捲曲或Twitter的OAuth庫需要):

  1. 註冊應用程式/網頁https://apps.twitter.com(您可能需要驗證您的個人帳戶移動數過)

  2. 注使用者密鑰和祕密

  3. PHP代碼:

    // auth parameters 
    $api_key = urlencode('REPLACEWITHAPPAPIKEY'); // Consumer Key (API Key) 
    $api_secret = urlencode('REPLACEWITHAPPAPISECRET'); // Consumer Secret (API Secret) 
    $auth_url = 'https://api.twitter.com/oauth2/token'; 
    
    // what we want? 
    $data_username = 'Independent'; // username 
    $data_count = 10; // number of tweets 
    $data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended'; 
    
    // get api access token 
    $api_credentials = base64_encode($api_key.':'.$api_secret); 
    
    $auth_headers = 'Authorization: Basic '.$api_credentials."\r\n". 
           'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n"; 
    
    $auth_context = stream_context_create(
        array(
         'http' => array(
          'header' => $auth_headers, 
          'method' => 'POST', 
          'content'=> http_build_query(array('grant_type' => 'client_credentials',)), 
         ) 
        ) 
    ); 
    
    $auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true); 
    $auth_token = $auth_response['access_token']; 
    
    // get tweets 
    $data_context = stream_context_create(array('http' => array('header' => 'Authorization: Bearer '.$auth_token."\r\n",))); 
    
    $data = json_decode(file_get_contents($data_url.'&count='.$data_count.'&screen_name='.urlencode($data_username), 0, $data_context), true); 
    
    // result - do what you want 
    print('<pre>'); 
    print_r($data); 
    

測試與XAMPP for Windows和Centos6默認安裝(PHP 5.3)

最有可能的問題,這可能是OpenSSL是不是在php.ini

要解決啓用檢查是否延長= php_openssl.dll或擴展名= php_openssl.so行存在並取消註釋在php.ini

+0

謝謝@Rauli!如果可以的話,我會加倍努力:)注意別人:目前,Twitter訪問令牌不會過期:http://stackoverflow.com/questions/8357568/do-twitter-access-token-expire,https:// dev。 twitter.com/oauth/overview/faq – Ben

相關問題