【问题标题】:how to post tweet using twitter 1.1 api and twitteroauth如何使用 twitter 1.1 api 和 twitteroauth 发布推文
【发布时间】:2013-06-08 12:37:41
【问题描述】:

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

【问题讨论】:

    标签: php twitter twitter-oauth


    【解决方案1】:

    当新的 API 破坏它时,我正在使用 twitteroauth.php 自己发布推文。您正确使用了$connection-&gt;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,您无需提供用户名/密码。身份验证令牌将处理一切。

    【讨论】:

      【解决方案2】:

      只用例子:

      $connection->post('statuses/update', array('status' =>$message));
      

      【讨论】:

      • 我们会得到推文 ID 和时间作为响应吗?
      • 我想通过'status'传递另一个'url'参数是否可能
      【解决方案3】:

      试试看 您不需要用户名/密码,您可以通过 API 密钥发布,请阅读此处的教程。

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

      【讨论】:

        【解决方案4】:

        问题是关于编码需要编码的值:

        例子

        $status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => rawurlencode($message)));
        

        如果你签入图书馆推荐https://github.com/J7mbo/twitter-api-php

        他们编码参数的方式

        【讨论】:

          猜你喜欢
          • 2013-06-09
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          • 2013-08-19
          • 1970-01-01
          • 2012-10-27
          • 2014-09-12
          • 1970-01-01
          相关资源
          最近更新 更多