2011-01-12 25 views
9

如何在推特上從我的網站發佈推文?我正在使用PHP腳本。無論我從我的網站發送的任何推文都應該更新我的Twitter帳戶。我使用下面的代碼,但它不是我的Twitter帳戶更新:如何在推特上使用PHP推文

// Set username and password 
$username='myusername'; 
$password='*********'; 
// The message you want to send 
$message = 'Nice to c all again.Have a nice day..'; 
// The twitter API address 
$url='http://twitter.com/statuses/update.xml'; 
// Alternative JSON version 
// $url = 'http://twitter.com/statuses/update.json'; 
// Set up and execute the curl process 
$curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, $url); 
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_POST,1); 
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,"status=".$message); 
curl_setopt($curl_handle, CURLOPT_USERPWD,"$username:$password"); 
$buffer = curl_exec($curl_handle); 
curl_close($curl_handle); 
// check for success or failure 
if (empty($buffer)) { 
    echo 'Try again'; 
} else { 
    echo 'success'; 
} 

該腳本返回一個成功的消息,但是當我檢查我的Twitter帳戶沒有推特被發現。

可能是什麼問題?

+0

使用類似'twitter php'的關鍵字的谷歌會扔掉很多鏈接。一些有用的鏈接是:http://dev.twitter.com/pages/libraries#php http://code.google.com/p/php-twitter/您尚未添加腳本。 – thotheolh 2011-01-12 05:49:49

回答

18

您正嘗試使用基本身份驗證(用戶名和密碼)發送推文。這不再被允許。網上有很多這樣的例子,但Twitter在去年8月關閉了它。您現在必須使用OAuth進行身份驗證。

0

鳴叫使用Twitter,你需要沿着post_authenticity_token用戶名密碼。 這個令牌可以通過使用curl來獲取你的個人資料頁面(在用curl登錄後)。我嘗試了捲曲並能夠使用捲曲進行推文。你可以找到我的代碼(儘管它在bash腳本中,它可以輕鬆移植到php,因爲它們都使用curlhttp://pastebin.com/a5eBcEeP