如何回复在他们的推文中提到我的推文

时间:2017-09-13 05:22:09

标签: php api twitter twitter-rest-api

我正在尝试以编程方式回复推文,那些在推文中提到我的推文。所以我收集了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);

?>

从上面的程序中,我得到了输出,提到我的推文集。现在我可以通过API回复他们的推文了。 这该怎么做?给我一些关于它的想法。如果您需要任何信息,请在评论中询问我。我很快就会回复你。

谢谢你。

0 个答案:

没有答案