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 '
'; 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 ' '; print_r($arrResult); die; $accessToken = $arrResult['access_token']; return $accessToken; }