2011-07-06 30 views
1

我有什麼:在Twitter和PHP應用程序在本地主機郵政/更新數據

創建 Twitter的應用程序,我想什麼:通過Twitter API 後鳴叫/更新狀態僅從應用程序帳戶(!) 。 沒有Twitter @任何地方,沒有用戶登錄,沒有什麼幻想。

我試了一下:

https://github.com/abraham/twitteroauth從dev.twitter.com網站訪問令牌,返回

「POST數據不可用此 令牌」

或類似的東西。

代碼片段我在一些網站上找到:

// Define credentials for the Twitter account 
define('TWITTER_CREDENTIALS', 'username:password'); 


// Set up CURL with the Twitter URL and some options 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/update.xml?status=test_status'); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


// Twitter uses HTTP authentication, so tell CURL to send our Twitter account details 
curl_setopt($ch, CURLOPT_USERPWD, TWITTER_CREDENTIALS); 


// Execute the curl process and then close 
$data = curl_exec($ch); 
curl_close($ch); 


// If nothing went wrong, we should now have some XML data 
echo '<pre>'.htmlentities(print_r($data, true)).'</pre>'; 

返回

「基本身份驗證是不是 支持」

回答

0

好吧,我發現有什麼問題。

默認情況下,Twitter將所有應用程序設置爲只讀,所以即使通過PHP登錄,也無法發送POST/UPDATE。你需要做的是去你的應用程序設置(就是你設置的應用程序名稱,描述和頭像)和頭像前有一個「默認訪問類型」,切換到

讀,寫,&直接消息

和瞧。

測試應用程序使用此代碼段

$twitter = new \TwitterOAuth('consumer_key', 'consumer_secret', 
           '(oauth_token', 'oauth_token_secret'); 
$twitter->get('account/verify_credentials'); 
$twitter->post('statuses/update', array('status' => 'test status')); 

哦,你不需要做登錄的東西與你的應用程序,你在「我的訪問令牌」菜單中選擇所需的令牌。

0

那大代碼段使用基本身份驗證,這是不再被Twitter支持。無視它。

現在,Twitter需要使用OAuth,而這正是abraham的twitteroauth庫所使用的。您必須使用此登錄/身份驗證,至少一次。

您還必須先到register your app with Twitter,才能收到您自己的消費者密鑰和消費者密鑰。然後你就可以通過twitteroauth開始使用Twitter的API了。

+0

是的我已經註冊了我的應用程序與Twitter,不能使用沒有這些鍵的twitteroauth。當您轉到應用程序的設置面板時,您也會獲得應用程序的令牌。 – Inoryy