•  0
    Ajax

    Get the tweet details using tweet id in php

      Asmin Jismun    36        0        Report content

    Obtain the tweet details using the twitter API

    function getTwitterInfo($tweetIds){
        //$tweetIds = A comma separated list of Tweet IDs, up to 100 are allowed in a single request.
        if(isset($_SESSION['twitter_access_token'])){
            $accessToken = $_SESSION['twitter_access_token'];
        } else {
            $accessToken = getTwitterAccessToken();
            $_SESSION['twitter_access_token'] = $accessToken;
        }

        $url = "https://api.twitter.com/1.1/statuses/lookup.json";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        $arrHeaders = array(
            'Authorization: Bearer '.$accessToken,
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
        );

        $arrParams = "id=".$tweetIds;
        curl_setopt($curl, CURLOPT_HTTPHEADER, $arrHeaders);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $arrParams);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        $result = curl_exec($curl);
        curl_close($curl);

        $arrResult = objectToArray(json_decode($result));
        $tweetIdList = explode(",",$tweetIds);
        $tweetInfoList = array();
        if(!isset($arrResult['errors'])){
            foreach($arrResult as $data){
                if(in_array($data['id_str'],$tweetIdList)){
                    $tweetInfoList["TW:".$data['id_str']] = $data['text'];
                }
            }
        }
        //echo '<pre>'; print_r($arrResult); die;
        return $tweetInfoList;
    }

    function getTwitterAccessToken(){
        $key = "your twitter app key";
        $secret = "yout twitter app secret";
        $token = base64_encode($key.":".$secret);

        $url = "https://api.twitter.com/oauth2/token";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        $arrHeaders = array(
            'Authorization: Basic '.$token,
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
        );

        $arrParams = "grant_type=client_credentials";
        curl_setopt($curl, CURLOPT_HTTPHEADER, $arrHeaders);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $arrParams);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        $result = curl_exec($curl);
        curl_close($curl);

        $arrResult = objectToArray(json_decode($result));
        //echo '<pre>'; print_r($arrResult); die;
        $accessToken = $arrResult['access_token'];
        return $accessToken;
    }

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations