2015-04-20 76 views
0

我無法從twitter API訪問media_url。我知道它在媒體陣列中,我相信媒體陣列是在一個實體陣列中。我只是無法獲得正確的語法。我已經嘗試向用戶數組創建一個類似的數組,但這不起作用。我也嘗試製作一個像用戶數組一樣的實體數組,但是數組返回空。從twitter API訪問media_url

<?php 
 
    ini_set('display_errors', 1); 
 
    require_once('TwitterAPIExchange.php'); 
 

 
    /** Set access tokens here - see: https://dev.twitter.com/apps/ **/ 
 
    $settings = array(
 
     'oauth_access_token' => "XXX-XXXXXXXX", 
 
     'oauth_access_token_secret' => "XXXXXXXX", 
 
     'consumer_key' => "XXXXX", 
 
     'consumer_secret' => "XXXX" 
 
); 
 

 
    /** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/ 
 
    $url = 'https://api.twitter.com/1.1/blocks/create.json'; 
 
    $requestMethod = 'GET'; 
 

 
    /** POST fields required by the URL above. See relevant docs as above **/ 
 
    $postfields = array(
 
     'screen_name' => 'usernameToBlock', 
 
     'skip_status' => '1' 
 
); 
 

 
    /** Perform a POST request and echo the response 
 
e response **/ 
 
    /** Note: Set the GET field BEFORE calling buildOauth(); **/ 
 
    $url = 'https://api.twitter.com/1.1/search/tweets.json'; 
 
    $getfield = '?q=%23blacklivesmatter'; 
 
    $requestMethod = 'GET'; 
 
    $twitter = new TwitterAPIExchange($settings); 
 

 

 
    $tweetData = json_decode($twitter->setGetField($getfield) 
 
       ->buildOauth($url, $requestMethod) 
 
       ->performRequest(),$assoc = TRUE); 
 

 
    echo $tweetData; 
 

 

 
    foreach($tweetData['statuses'] as $items) 
 
     { 
 

 
     $userArray = $items['user']; 
 
     $entitiesArray = $items['entities']; 
 
     $mediaArray = $entitiesArray['media']; 
 
     $date = new DateTime($items->created_at); 
 

 

 
     echo "<div class='tweet-div'><div class='float-left twitpic'><a target='_blank' href='http://www.twitter.com/" . $userArray['screen_name'] . "'><img class='twitter-pic' target='_blank' src='" . $userArray['profile_image_url'] . "'></a></div>"; 
 
     echo "<a target='_blank' href='http://www.twitter.com/" . $userArray['screen_name'] . "'><span class='name bold'>" . $userArray['name'] . "</span> </br><span class='handle'>@" . $userArray['screen_name'] . "</span></a> <span class='font-small'>&sdot; "; 
 
     echo $date->format('M jS') . "</span></br>"; 
 
     echo $items['text'] . "</br></br>"; 
 
     echo "<img target='_blank' src='http://" . $mediaArray['media_url'] . "'></br></br>"; 
 
     echo $mediaArray['media_url'] . ""; 
 
     echo "<span class='border'></span></div>"; 
 

 

 
     } 
 

 

 

 
    echo $twitter->setGetfield($getfield) 
 
       ->buildOauth($url, $requestMethod) 
 
       ->performRequest(); 
 

 
    ?>

回答

0

當加載鳴叫具有查詢,從此路徑

$mm1= $tweetData['statuses']["0"]['entities']['media']['0']['media_url']; 
echo $mm1; 

得到MEDIA_URL,你將有正確的數據附加到鳴叫(第一個)圖像。如果你有更多的圖像,你可以增加這個數字['0']並找到更多。

我不得不浪費數小時才能獲得適當的圖像以顯示推文狀態(文本) 我還看到有些媒體沒有['0'],但在'媒體'之後直接使用'media_url'。我的方法可能不完美,但訪問所有媒體,但我的目的已解決。 祝你好運