如何使用 PHP 在 Twitter 上发推文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:54:05  来源:igfitidea点击:

How to Tweet in Twitter using PHP

phptwitter

提问by Senthil

How can I tweet in twitter from my website? I am using a PHP script. 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 上发推文?我正在使用 PHP 脚本。我从我的网站发送的任何推文都应该更新我的推特账户。我使用以下代码,但它没有在我的 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?

可能是什么问题呢?

回答by Adam Green

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.

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

回答by Sahil Singh

to tweet using twitter you will need a post_authenticity_tokenalong with your usernameand password. this token can be obtained from your profile page by fetching it using curl (after you login with 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.

要使用 twitter 发推文,您需要一个post_authenticity_token以及您的用户名密码。可以通过使用 curl 获取它从您的个人资料页面获取此令牌(在您使用 curl 登录后)。我尝试了 curl 并且能够使用 curl 发推文。你可以在(虽然它是在 bash 脚本中,但它可以很容易地移植到 php 因为他们都使用curlhttp://pastebin.com/a5eBcEeP找到我的代码。

回答by Metablocks Corp

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 和 1.1 版本的 Twitter API 编写推文函数:https: //dev.twitter.com/docs/twitter-libraries

tmhOAuthis probably my favorite.

tmhOAuth可能是我最喜欢的。