6

我使用下面的代码来检索我的推文并回显 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));
?>
4

4 回答 4

15

当新的 API 破坏它时,我正在使用 twitteroauth.php 自己发布推文。您使用$connection->post正确,但似乎该功能不再起作用。我发现的最简单的解决方案是将 twitteroauth.php 替换为 J7mbo 的 twitter-api-php 文件以获取新的 1.1 API:

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:

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

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.

于 2013-07-31T03:46:37.263 回答
5

Just use the example:

$connection->post('statuses/update', array('status' =>$message));
于 2013-09-05T10:38:41.620 回答
1

Try it you won't need to username/password you can post via API key read the tutorial here.

http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/

于 2014-10-15T07:53:09.507 回答
-2

The issue is about enconding the value need to be 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

That the way they encode the params

于 2016-09-23T18:17:00.937 回答