Rachel Morris
Rachel Morris

Reputation: 127

Retrieving direct messages from twitter using PHP?

I am trying to retrieve direct messages sent to my account real time. But the way, my script is working is that when someone sends me a message, I have to refresh the script to get the latest message. I would like to make it real time such that whenever someone sends me a message, I get it instantly. How do I go about it? Any help/suggestions is highly appreciated.

<?php
ini_set('display_errors', 1);
require_once('twitterapiexchange.php'); 
require_once("twitteroauth.php");
$settings = array( 
    'oauth_access_token' => "*********",
    'oauth_access_token_secret' => "*********",
    'consumer_key' => "*********",
    'consumer_secret' => "*********");
$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);
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 />";
    $senderId = $items['sender_id'];
    $messageText = $items['text'];
    $timeStamp = $items['created_at'];
    $recipentId = $items['recipient_id'];
    $messageId = $items['id'];
    $screenname=$items['sender_screen_name'];
    if(!empty($messageText)) {
        // $data = file_get_contents('https://fb.craftsilicon.com/testphpbot/core.php??username=".$senderId."&tweetid=".$messageId."&tweettext=".$messageText."&tweettime="$timeStamp."&source=facebook&receiver=".$recipentId."');
        $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452');
        $obj = json_decode($json);
        //var_dump($obj->results[0]->formatted_address);
        $addr = $obj->results[0]->formatted_address;
        $answer = "You said ".$messageText . " " .$addr;
        echo "<strong>Answer:</strong> ".$answer."<br />";
        $api_key='*********' ;
        $api_secret= '*********' ;
        $access_token = '*********';
        $access_token_key='*********' ;
        $connection = new TwitterOAuth($api_key,$api_secret, $access_token, $access_token_key);
        $connection->post('direct_messages/new', array('user_id' => $senderId, 'text' => $messageText));
    }
    //var_dump(json_encode($items, true));
}?>

Upvotes: 1

Views: 360

Answers (1)

anon
anon

Reputation:

You'd need to use the streaming API and maintain a continuous connection to a user stream, then filter out direct messages from the responses.

Upvotes: 1

Related Questions