skip to Main Content

i need to display/show photo with twitter api, but i have only get 1 photo

$results = $toa->get('https://api.twitter.com/1.1/statuses/show.json?id=xxxxxxxxxxxx' );



if (isset($results->entities->media)) {
foreach ($results->entities->media as $media) {
$media_url = $media->media_url; // Or $media->media_url_https for the SSL version.
}
}

echo $media_url;

this code get only 1 photo ,how to get pulling multiple image (max 4) on 1 twitter status ?

thank

2

Answers


  1. It looks like you’re overwriting the $media_url variable on each loop. You would want to loop through the $results->entities->media variable when displaying the images like this:

    foreach ($results->entities->media as $media) {
        echo '<img src="'.$media->media_url.'"/>';
    }
    
    Login or Signup to reply.
  2. I don’t know too much about the Twitter API however you are only getting one photo at the end because you are doing this.

    Inside the loop you keep updating $media_url to $media->media_url. Then outside the loop you echo $media_url only once.

    All you are doing is changing that one variable over and over, you need to have your echo statement inside the loop.

    if (isset($results->entities->media)) {
        foreach ($results->entities->media as $media) {
            $media_url = $media->media_url;
            echo $media_url;
        }
    }
    

    Like I said I don’t have too much knowledge of the Twitter API but providing your loop is working to get one of the images then moving the echo statement inside the loop should help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search