我需要解码并专门定位此数组中的第一个网址:
[{
"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"
}
]
我正在使用的脚本可以抓取最后一个或者一个随机网址,但正如我所说,我真的需要让它只针对第一个网址 - 有人可以修改我的脚本以便我能够实现这一点。
<?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');
}
?>
我真的很感激任何帮助。
答案 0 :(得分:0)
嗯,这是your code, as is, displays the correct url。
的证明为了清楚起见,输出为/Pics/ob/7d778-127a9294cec0-12a929779a2b.Img.jpeg
(第一个网址),而不是/Pics/ob/7d778-12a918226470-12a91784f47a.Img.jpeg
(最后一个网址)。
However, I would just use $array_of_objects[0]->url;
。
因此,您的代码工作正常,但您的解码有点复杂。取代
$array_of_objects = json_decode($obj);
$object = $array_of_objects[0];
$url = ($object->url);
使用:
$array_of_objects = json_decode($obj);
$url = $array_of_objects[0]->url;
更重要的是,我会测试你的copy()
确实有效,使用类似的东西:
if ( !copy($img, $radar_dir.'test.png') )
{
echo "failed to copy $img...\n";
}
此时似乎$img
是jpeg
的网址,您将其复制到.../test.png
,fwiw。