views:

23

answers:

1

Hi, I need to decode and specifically target the first url only in this array:

[{
"longDateTime":"3:00pm Saturday 21 August 2010",
"shortDateTime":"3:00pm Sat",
"url":"\/Pics\/ob\/7d778-127a9294cec0-12a929779a2b.Img.jpeg"
},{
"longDateTime":"2:00pm Saturday 21 August 2010",
"shortDateTime":"2:00pm Sat",
"url":"\/Pics\/ob\/7d778-12a9275de040-12a92760c93c.Img.jpeg"
},{
"longDateTime":"1:00pm Saturday 21 August 2010",
"shortDateTime":"1:00pm Sat",
"url":"\/Pics\/ob\/7d778-12a79226f1c0-12a79229bb4c.Img.jpeg"
},{
"longDateTime":"12:00pm Saturday 21 August 2010",
"shortDateTime":"12:00pm Sat",
"url":"\/Pics\/ob\/7d778-12a917f00340-12a91f3437fd.Img.jpeg"
},{
"longDateTime":"11:00am Saturday 21 August 2010",
"shortDateTime":"11:00am Sat",
"url":"\/Pics\/ob\/7d778-12a91b914c70-172a91bf8987.Img.jpeg"
},{
"longDateTime":"10:00am Saturday 21 August 2010",
"shortDateTime":"10:00am Sat",
"url":"\/Pics\/ob\/7d778-12a918226470-12a91784f47a.Img.jpeg"
}
]

The script that I am using works to grab the last or maybe a random url but as I said I really need to get it to target the first url only - can someone please modify my script so that I can achieve this please.

<?php
$radar_dir='./radar/';



    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'http://somesite.com/public/test');
    $fp = fopen($radar_dir.'test.txt', 'w');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec ($ch);
    curl_close ($ch);
    fclose($fp);

?>




<?php
{

            $txt_file = $radar_dir.'test.txt';
            if(file_exists($txt_file)==false)
            $img = $error_img;
        else
        {
            $handle = fopen($txt_file, 'r');
            $obj = fread($handle,filesize($txt_file));
            $array_of_objects = json_decode($obj);
            $object = $array_of_objects[0];
            $url = ($object->url); 
            $img =   "http://somesite.com" . $url;
        }
        copy($img,$radar_dir.'test.png');

}
?>

I would really appreciate any help with this.

A: 

Well, here is proof that your code, as is, displays the correct url.

Just to be clear, the output is /Pics/ob/7d778-127a9294cec0-12a929779a2b.Img.jpeg (the 1st url) and not, /Pics/ob/7d778-12a918226470-12a91784f47a.Img.jpeg (the last url).

However, I would just use $array_of_objects[0]->url; directly.

So, your code works fine, but your decode is a little over complicated. Replace

$array_of_objects = json_decode($obj);
$object = $array_of_objects[0];
$url = ($object->url);

With:

$array_of_objects = json_decode($obj);
$url = $array_of_objects[0]->url;

What is more important, is that I would test that your copy() actually works, using something like:

if ( !copy($img, $radar_dir.'test.png') ) 
{
    echo "failed to copy $img...\n";
}

At this point it seems that $img is a url of a jpeg, and you're copying it to .../test.png, fwiw.

Peter Ajtai
Thanks will do.
Gary
Its working fine with the copy() - just added a text overlay and thumbnail script to it so hopefully this time tomorrow my folder will have 24 images ready to display as a slideshow.
Gary