通过twitter API发布/更新数据


Post/update data via twitter API

我有什么:Twitter应用创建于Twitter, PHP应用创建于localhost

我想要的:通过twitter API发布tweet/更新状态,仅从(!)应用程序帐户。没有twitter@anywhere,没有用户登录,没有什么花哨的

What I tried:

https://github.com/abraham/twitteroauth带有来自dev.twitter.com网站的访问令牌,返回

"POST数据不可用令牌"

之类的

代码片段,我发现在一些网站:

// Define credentials for the Twitter account
define('TWITTER_CREDENTIALS', 'username:password');

// Set up CURL with the Twitter URL and some options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/update.xml?status=test_status');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Twitter uses HTTP authentication, so tell CURL to send our Twitter account details
curl_setopt($ch, CURLOPT_USERPWD, TWITTER_CREDENTIALS);

// Execute the curl process and then close
$data = curl_exec($ch);
curl_close($ch);

// If nothing went wrong, we should now have some XML data 
echo '<pre>'.htmlentities(print_r($data, true)).'</pre>';   

返回

"基本身份验证不是支持"

这个大代码片段使用基本身份验证,而Twitter不再支持这种身份验证。置之不理。

Twitter现在需要

OAuth,这就是abraham的twitteroauth库所使用的。您必须使用此登录/认证,至少一次。

您还必须首先在Twitter上注册您的应用程序,以接收您自己的消费者密钥和消费者秘密。然后你就可以通过twitteroauth开始使用Twitter的API了。

好的,我找到问题所在了。

默认情况下,Twitter将所有应用设置为只读,所以即使你通过PHP登录,你也不能发送POST/UPDATE。你需要做的是进入你的应用程序设置(你设置应用程序名称,描述和头像的地方),在头像之前有一个"默认访问类型",把它切换到

Read, Write, &直接消息

瞧。

使用下面的代码片段

来测试应用程序
$twitter = new 'TwitterOAuth('consumer_key', 'consumer_secret', 
                              '(oauth_token', 'oauth_token_secret');
$twitter->get('account/verify_credentials');
$twitter->post('statuses/update', array('status' => 'test status'));

哦,你不需要在你的应用程序上做登录的事情,你可以在"我的访问令牌"菜单下获得所需的令牌