Blogger Information
Blog 4
fans 0
comment 1
visits 5112
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用PHP通过twitter api获取twitter的推文信息
董辉哦呵呵
Original
1489 people have browsed it

如果想用twitter api获取twitter的推文信息,首先你需要在https://apps.twitter.com

上面创建一个而应用,然后填写应用的相关信息如下图:

DLDGA76VL666O@`V)WGNY_U.png

通过创建应用获取 Consumer Key ,Consumer Secret,Access Token,Access TokenSEQ%3A%QJO@IB{UF{ZPYVFX.png

LI_1@S3$M_YZOWO5(DQ9T@W.png

<?php
    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;
    }
    function returnTweet(){
        $oauth_access_token         = "970871825709244417-77xNRiOhc9t1ugXcaLm1XdP0HSaYv4J";
        $oauth_access_token_secret  = "D9MO2Xux4YciTh0z91nLfQoFS7bDZp7zoSMDH37cpBVHk";
        $consumer_key               = "JPflgwtDYFghOO6zjXJqcPzeE";
        $consumer_secret            = "0h2qs174patwTWC2hy6XfqsfnhlJvVBWZUnQja7FTGs4p5Y7BY";
        $twitter_timeline           = "user_timeline";  //  mentions_timeline / user_timeline / home_timeline / retweets_of_me
        //  create request
            $request = array(
                'screen_name'       => 'GbrilliantQ',
                'count'             => '3'
            );
        $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'
        );
        //  merge request and oauth to one array
            $oauth = array_merge($oauth, $request);
        //  do some magic
            $base_info              = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
            $header = array(buildAuthorizationHeader($oauth), 'Expect:');
            $options = array( CURLOPT_HTTPHEADER => $header,
                              CURLOPT_HEADER => false,
                              CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
                              CURLOPT_RETURNTRANSFER => true,
                              CURLOPT_SSL_VERIFYPEER => false);
            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);
        return $json;
    }
    $tweet = returnTweet();
    echo $tweet;
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post