我使用以下代码检索我的推文并回显 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));
?>
最佳答案
当新 API 损坏时,我正在使用 twitteroauth.php 自己发布推文。您正在正确使用 $connection->post
,但该功能似乎不再起作用。我找到的最简单的解决方案是将 twitteroauth.php 替换为 J7mbo 的 twitter-api-php 文件以用于新的 1.1 API:
https://github.com/J7mbo/twitter-api-php
这是他的分步使用说明。我认为您会惊喜地发现您可以保留大部分代码不变,只需在适当的地方将 twitteroauth 调用与其函数调用切换即可:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
他没有提供发布推文的具体示例,但这是该功能所需的:
$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,您将不需要提供您的用户名/密码。身份验证 token 将处理一切。
关于php - 如何使用 twitter 1.1 api 和 twitteroauth 发布推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17064834/