php 如何使用 twitter 1.1 api 和 twitteroauth 发布推文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17064834/
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
how to post tweet using twitter 1.1 api and twitteroauth
提问by Susheel Singh
I use the below code to retrieve my tweets and echo json. This works fine.
我使用下面的代码来检索我的推文和回显 json。这工作正常。
<?php
session_start();
require_once('includes/twitter/twitteroauth.php');
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
?>
Now i want to send a tweet using the similar code but it doesnot work. I am not sure if the sending syntax is correct. so please someone help me.
现在我想使用类似的代码发送一条推文,但它不起作用。我不确定发送语法是否正确。所以请有人帮助我。
<?php
session_start();
require_once('includes/twitter/twitteroauth.php'); //Path to twitteroauth library
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
// start connection
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
//message
$message = "Hi how are you";
//send message
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => $message));
?>
回答by ideonexus
I was using twitteroauth.php to post tweets myself when the new API broke it. You are using the $connection->post
correctly, but it appears that function does not work anymore. The easiest solution I found was to swap out the twitteroauth.php with J7mbo's twitter-api-php file for the new 1.1 API:
当新 API 破坏它时,我正在使用 twitteroauth.php 自己发布推文。您正在$connection->post
正确使用,但该功能似乎不再起作用。我找到的最简单的解决方案是将 twitteroauth.php 与 J7mbo 的 twitter-api-php 文件替换为新的 1.1 API:
https://github.com/J7mbo/twitter-api-php
https://github.com/J7mbo/twitter-api-php
Here's his step-by-step instructions for using it. I think you'll be pleasantly surprised to find you can leave most of your code the same, just switch the twitteroauth calls with his function calls at the appropriate places:
这是他使用它的分步说明。我想你会惊喜地发现你可以让你的大部分代码保持不变,只需在适当的地方切换 twitteroauth 调用和他的函数调用:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
使用 Twitter API 1.1 版检索 user_timeline 的最简单 PHP 示例
He doesn't provide the specific example of posting a tweet, but here's what what you need for that function:
他没有提供发布推文的具体示例,但这是该功能所需的内容:
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Hi how are you' );
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
With the new twitter API, you won't need to provide your username/password. The authentication token will handle everything.
使用新的 twitter API,您无需提供用户名/密码。身份验证令牌将处理一切。
回答by Noelchan
Just use the example:
只需使用示例:
$connection->post('statuses/update', array('status' =>$message));
回答by Savan Paun
Try it you won't need to username/password you can post via API key read the tutorial here.
试试看,您不需要用户名/密码,您可以通过 API 密钥发布,请阅读此处的教程。
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
回答by Luis Alvarez
The issue is about enconding the value need to be enconde :
问题是关于需要 enconde 的值:
Example
例子
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => rawurlencode($message)));
If you check in the library recomended https://github.com/J7mbo/twitter-api-php
如果您签入库推荐https://github.com/J7mbo/twitter-api-php
That the way they encode the params
他们编码参数的方式