How to reply to the tweets which have mentioned me in their tweets

58 views Asked by At

I'm trying to reply to tweets programmatically, those which have mentioned me in their tweets. So I collected the tweets which have mentioned me from the mention_timeline Api

mentioned_me_tweets.php

<?php

    ini_set('displays_error',1);
    error_reporting(E_ALL);


    function buildBaseString($baseURI, $method, $params) {
        $r = array();
        ksort($params);
        foreach($params as $key=>$value){
            $r[] = "$key=" . rawurlencode($value);
        }
        return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
    }

    function buildAuthorizationHeader($oauth) {
        $r = 'Authorization: OAuth ';
        $values = array();
        foreach($oauth as $key=>$value)
            $values[] = "$key=\"" . rawurlencode($value) . "\"";
        $r .= implode(', ', $values);
        return $r;
    }

    //$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    $url = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";

    $oauth_access_token = "xxxx";
    $oauth_access_token_secret = "yyyy";
    $consumer_key = "zzzzzz";
    $consumer_secret = "ccccc";

    $oauth = array( 'oauth_consumer_key' => $consumer_key,
                    'oauth_nonce' => time(),
                    'oauth_signature_method' => 'HMAC-SHA1',
                    'oauth_token' => $oauth_access_token,
                    'oauth_timestamp' => time(),
                    'oauth_version' => '1.0');

    $base_info = buildBaseString($url, 'GET', $oauth);
    $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
    $oauth['oauth_signature'] = $oauth_signature;


    // Make requests
    $header = array(buildAuthorizationHeader($oauth), 'Expect:');
    $options = array( CURLOPT_HTTPHEADER => $header,
                      //CURLOPT_POSTFIELDS => $postfields,
                      CURLOPT_HEADER => false,
                      CURLOPT_URL => $url,
                      CURLOPT_RETURNTRANSFER => true,
                      CURLOPT_SSL_VERIFYPEER => false);

    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $json = curl_exec($feed);
    curl_close($feed);

    $twitter_data = json_decode($json);

//print it out
echo "<pre>";
print_r($twitter_data);

?>

From the above program, I get as output, the collection of tweets which have mentioned me. Now I can reply to their tweets via an API. How to do this? give me some ideas about it. If you want any information, ask me in comments. I will reply to you soon.

Thanks advance .

0

There are 0 answers