如何使用 twitter 1.1 api 和 twitteroauth 发布推文 [英] how to post tweet using twitter 1.1 api and twitteroauth

查看:57
本文介绍了如何使用 twitter 1.1 api 和 twitteroauth 发布推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来检索我的推文并回显 json.这工作正常.

I use the below code to retrieve my tweets and echo json. This works fine.

<?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));
?>

推荐答案

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

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:

https://github.com/J7mbo/twitter-api-php

这是他使用它的分步说明.我想你会惊喜地发现你可以让你的大部分代码保持不变,只需在适当的地方切换 twitteroauth 调用和他的函数调用:

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:

使用 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();

使用新的 Twitter API,您无需提供用户名/密码.身份验证令牌将处理所有事情.

With the new twitter API, you won't need to provide your username/password. The authentication token will handle everything.

这篇关于如何使用 twitter 1.1 api 和 twitteroauth 发布推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆