Search code examples
phpxmltumblr

Get 3 URLs from XML, randomly select one to save to variable


I'm in over my head at this point - but what i have currently looks like this:

<?php
    $request_url = "http://aethereverywhere.tumblr.com/api/read?type=photo&tagged=ae&start=0&num=1";
    $xml = simplexml_load_file($request_url);
    $img = $xml->posts->post->{'photo-url'};
?>

If increase &num to 3, lets say - it'll pull three files, and simplexml_load_file will parse them out - and save them to $img - but what i'd like to is have only one URL saved to $img, selected at random.

Thanks for the help


Solution

  • New code: select a random image from 0 to 118 (total 119), then outputing, choose the highest resolution.

    <?php
        $request_url = "http://aethereverywhere.tumblr.com/api/read?type=photo&start=".rand(0,118);
        $xml = simplexml_load_file($request_url);
        $img = $xml->posts->post->{'photo-url'};
        $img=(array)$img;
    
        echo '<img src="'.$img[0].'">';
    ?>