1

I want to retrieve direct messages from twitter using TwitterAPIExchange.php. The settings in my application are: write/read and direct messages. What I am missing? Merry Christmas

Updated Code

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

Solved. Please read this code that could help you.

 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));
        }
3
  • What is the output? Any errors?
    – DAG
    Commented Dec 24, 2013 at 10:49
  • any erro.. you can see it in:turpin.es/turpin/direct.php Commented Dec 24, 2013 at 10:54
  • I have updated the code with more details Commented Dec 24, 2013 at 11:05

1 Answer 1

1

One glaring error in your code I see here is items is not a response object of direct_messages API. It's 'tweets'.

https://dev.twitter.com/docs/api/1.1/get/direct_messages and look for Response Object

Assuming your app has taken care of providing necessary permissions to the user of accessing direct messages, can you try modifying your for loop as following:

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

$assoc = TRUE is taking care of converting the object to associative array already. Now you need to iterate over the response objects contained in this array.

4
  • Thanks for your response. I´ve updated the code but I get: Error 32: Could not authenticate you Commented Dec 27, 2013 at 17:37
  • sure, that might be because $settings array. Can you check if these parameters are getting set fine - check against the values from your app @ dev.twitter.com would help! $settings = array( 'oauth_access_token' => $accessToken, 'oauth_access_token_secret' => $accessTokenSecret, 'consumer_key' => $consumerKey, 'consumer_secret' => $consumerKeySecret Commented Dec 27, 2013 at 17:41
  • also, please check if the app has write permissions as well. These 2 considerations would resolve your authentication issue. Commented Dec 27, 2013 at 17:46
  • 1
    Hi, thanks. My permissions are correct and $settings array too. I have revisited it. I´m trying a simple code and I think that there is a problem in the foreach: $responses = json_decode($response, true); if (is_array($responses)){ $users = $responses['tweets']; foreach($responses as $user) { echo 'tweet:' . $user['text'] . '</br>'; } } Commented Dec 27, 2013 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.