检索直接消息twitter api

时间:2013-12-24 10:48:45

标签: php api twitter

我想使用TwitterAPIExchange.php从twitter检索直接消息。我的应用程序中的设置是:写入/读取和直接消息。我错过了什么?圣诞快乐

更新代码

   $settings = array(
  'oauth_access_token' => $accessToken,
  'oauth_access_token_secret' => $accessTokenSecret,
  'consumer_key' => $consumerKey,
  'consumer_secret' => $consumerKeySecret
);
$i = 0;
$cursor = -1;

do {
  $url = 'https://api.twitter.com/1.1/direct_messages.json';
  $getfield = '?cursor='.$cursor.'since_id=240136858829479935&count=1';
  $requestMethod = 'GET';
  $twitter = new TwitterAPIExchange($settings);
  $response = $twitter->setGetfield($getfield)
                      ->buildOauth($url, $requestMethod)
                      ->performRequest();

  $response = json_decode($response, true);

  if (!empty($response["next_cursor_str"])) {
    $next_cursor = $response["next_cursor_str"];
  } else {
    $next_cursor = 0;
  }

  $errors = $response["errors"];

  if (!empty($errors)) {
    foreach($errors as $error){
      $code = $error['code'];
      $msg = $error['message'];
      echo "<br><br>Error " . $code . ": " . $msg;
    }
    $cursor = 0;
  } else {
      $users = $response['tweets'];
      foreach($users as $user) {
          echo 'tweet:'  .  $user['text'] . '</br>';
      }
  }
  if ($next_cursor != null) {
    $cursor = $next_cursor;
  }
} while ( $cursor != 0);

解决。 请阅读可以帮助您的代码。

 ini_set('display_errors', 1);
 require_once('TwitterAPIExchange.php'); 
$settings = array( 
    'oauth_access_token' => "xxx",
    'oauth_access_token_secret' => "xxx",
    'consumer_key' => "xxx",
    'consumer_secret' => "xxx");
  $url = 'https://api.twitter.com/1.1/direct_messages.json';
  $getfield = '?since_id=240136858829479935&count=1';
  $requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest(),$assoc = TRUE);

echo "<h3>Mensajer directos</h3>";          
foreach($string as $items)
 {
            $url = 'https://api.twitter.com/1.1/direct_messages/show.json?';
            $requestMethod = 'GET';
            $getfields = array('id' => $items['id']);
            $twitter = new TwitterAPIExchange($settings);           
            $do = $twitter->setGetfield($getfield)
                          ->buildOauth($url, $requestMethod)                         
                          ->performRequest();
            echo "<strong>Teet:</strong> ".$items['text']."<br />";
            var_dump(json_encode($items, true));
        }

1 个答案:

答案 0 :(得分:1)

我在这里看到的代码中的一个明显错误是items不是direct_messages API的响应对象。这是'推文'。

https://dev.twitter.com/docs/api/1.1/get/direct_messages并查找响应对象

假设您的应用已负责为用户提供访问直接消息的必要权限,您是否可以尝试修改 for 循环,如下所示:

...
foreach ( $string ['tweets'] as $items ) {
    echo  $items['text'];
    ?>

$assoc = TRUE正在处理将对象转换为关联数组的问题。现在,您需要遍历此数组中包含的响应对象。