我的程序需要做两个json调用。第一个提供一个id列表,然后在循环中一次一个地使用,以使用第二个调用检索数据。
第一个电话很有效,但第二个电话有问题。我知道我检索了json数据,因为我已经回复了它,但我似乎死在第二个json_decode,因为我看到回声“RETURNED:”然后“next”但我没有看到回声“DATA:”。我是否有某种语法错误或者是否以更严肃的方式解决了这个问题?
<?php
$crl = curl_init();
$timeout = 30;
curl_setopt($crl, CURLOPT_URL, "http://api.somesite.com/list?parm1=XXX&parm2=YYY&response_format=json");
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout );
$ret = curl_exec($crl);
curl_close($crl);
$data = json_decode($ret);
echo "<P>length: $dlen </p>";
$somedata = $data->body->somedatas;
$count = 0;
foreach ( $somedata as $somedata )
{
$id = $somedata->id;
echo "<br />ID: " . $id;
$command=""http://api.somesite.com/list2?id=$id&parm2=YYY&response_format=json";
$timeout = 30;
echo "<br />Trying...";
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $command );
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout );
$ret2 = curl_exec($crl);
echo "<br />RETURNED=" . $ret2;
curl_close($crl);
echo "<br />next";
$data2 = json_decode($ret2);
echo "<br />DATA=" . $data2;
// more code follows...
}
echo "<br />done";
echo "<hr></body></html>";
?>