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