简体   繁体   English

如何使用PHP在Twitter上发推文

[英]How to Tweet in Twitter using PHP

How can I tweet in twitter from my website? 我怎么能在我的网站上发布推文? I am using a PHP script. 我正在使用PHP脚本。 Whatever tweets I send from my website should update my twitter account. 我从我的网站发送的任何推文都应该更新我的推特账号。 I use the following code, but it is not updating in my twitter account: 我使用以下代码,但它没有在我的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';
}

This script is returning a success message, but when I check my twitter account no tweets are found. 此脚本返回成功消息,但是当我检查我的Twitter帐户时,没有找到推文。

What could be the problem? 可能是什么问题呢?

You are trying to send tweets using Basic Authentication (user name and password). 您正在尝试使用基本身份验证(用户名和密码)发送推文。 This is no longer allowed. 这不再允许。 There are many examples of this online, but Twitter turned it off last August. 网上有很多这样的例子,但是去年八月推特关闭了它。 You now have to use OAuth to do authentication. 您现在必须使用OAuth进行身份验证。

to tweet using twitter you will need a post_authenticity_token along with your username and password . 要使用Twitter发送推文,您需要一个post_authenticity_token以及您的用户名密码 this token can be obtained from your profile page by fetching it using curl (after you login with curl). 这个令牌可以通过使用curl获取(在您使用curl登录后)从您的个人资料页面获取。 i experimented with curl and was able to tweet using curl. 我尝试了卷曲,并能够使用卷曲发推文。 you can find my code at (though it is in bash script, it can be ported to php easily coz they both use curl ) http://pastebin.com/a5eBcEeP . 你可以找到我的代码(虽然它是在bash脚本中,它可以很容易地移植到php,因为它们都使用curlhttp://pastebin.com/a5eBcEeP

You can find a list of PHP libraries that support OAUTH and you can use to write a tweet function in PHP and the 1.1 version of the Twitter API here: https://dev.twitter.com/docs/twitter-libraries 您可以找到支持OAUTH的PHP库列表,您可以使用PHP编写推文函数和Twitter API的1.1版本: https//dev.twitter.com/docs/twitter-libraries

tmhOAuth is probably my favorite. tmhOAuth可能是我最喜欢的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM