如何使用PHP在Twitter上发布Tweet

如何使用PHP在Twitter上发布Tweet,php,twitter,Php,Twitter,我如何从我的网站在twitter上发推特?我正在使用一个PHP脚本。无论我从我的网站发送什么tweet,都应该更新我的twitter帐户。我使用以下代码,但它不会在我的twitter帐户中更新: // Set username and password $username='myusername'; $password='*********'; // The message you want to send $message = 'Nice to c all again.Have a nice

我如何从我的网站在twitter上发推特?我正在使用一个PHP脚本。无论我从我的网站发送什么tweet,都应该更新我的twitter帐户。我使用以下代码,但它不会在我的twitter帐户中更新:

// 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帐户时,未找到任何推文


可能是什么问题?

您正在尝试使用基本身份验证(用户名和密码)发送推文。这是不允许的。网上有很多这样的例子,但是Twitter在去年八月关闭了它。现在,您必须使用OAuth进行身份验证。

要使用twitter进行推特,您需要一个post\u Authentity\u令牌以及您的用户名密码。 通过使用curl(使用curl登录后)获取配置文件页面,可以从中获取该令牌。我尝试了curl,并且能够使用curl发tweet。您可以在找到我的代码(虽然它在bash脚本中,但可以很容易地移植到php,因为它们都使用curl)
.

您可以在此处找到支持OAUTH的PHP库列表,您可以使用这些库在PHP和1.1版本的Twitter API中编写tweet函数:


可能是我的最爱。

使用“twitter php”等关键词的谷歌会抛出大量链接。一些有用的链接是:您尚未添加脚本。第一个链接已断开。。使用