我使用下面的代码来检索我的tweet并回显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);
?>现在,我想使用类似的代码发送一条推文,但它不起作用。我不确定发送语法是否正确。所以请谁来帮帮我。
<?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));
?>发布于 2013-07-31 11:46:37
当我自己使用twitteroauth.php发布推文时,新的API破坏了它。您正在正确使用$connection->post,但该函数似乎不再起作用。我找到的最简单的解决方案是用新的1.1API替换J7mbo的twitter-api-php文件中的twitteroauth.php:
https://github.com/J7mbo/twitter-api-php
下面是他使用它的逐步说明。我想你会惊喜地发现,你可以保持大部分代码不变,只需在适当的地方将twitteroauth调用与他的函数调用进行切换:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
他没有提供发布tweet的具体示例,但以下是该函数所需的内容:
$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();使用新的twitter API,您将不需要提供用户名/密码。身份验证令牌将处理所有事情。
发布于 2013-09-05 18:38:42
只需使用下面的示例:
$connection->post('statuses/update', array('status' =>$message));发布于 2014-10-15 15:53:10
尝试一下,你不需要用户名/密码,你可以通过API key发布,请在这里阅读教程。
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
https://stackoverflow.com/questions/17064834
复制相似问题