带有freebase/location/contains的PHP

带有freebase/location/contains的PHP,php,html,arrays,json,freebase,Php,Html,Arrays,Json,Freebase,我有一个获取伦敦点(id、名称、地理位置)的php文件。问题是,我以json格式获得了正确的结果,但当我解码它并尝试获取结果的contains数组时,我得到了一个错误。如何从“/location/location/contains”属性获取数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

我有一个获取伦敦点(id、名称、地理位置)的php文件。问题是,我以json格式获得了正确的结果,但当我解码它并尝试获取结果的contains数组时,我得到了一个错误。如何从“/location/location/contains”属性获取数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>search</title>
</head>

<body>
 <?php
 function freebasequery ($fid){
 $query = array(array('id' => $fid, '/location/location/contains'=>array(array('id'=>NULL,'name' => NULL,'/location/location/geolocation' =>array(array('/location/geocode/longitude' =>NULL,'/location/geocode/latitude' => NULL))))));
$query_envelope = array('query' => $query);
$service_url = 'http://api.freebase.com/api/service/mqlread';
$url = $service_url . '?query=' . urlencode(json_encode($query_envelope));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
 }
 $points=freebasequery('/en/london');
//echo $points;
 $results=json_decode($points)->result;
foreach($results as $poi){
    echo $poi->id;
    $contains="/location/location/contains";
    $poisarray=$poi->$contains;
    foreach($poisarray as $point){
        echo $point->id;
    }
}


 ?>
 </body>
 </html>

搜索

它在
json\u decode
上出现的错误(需要有一个true)解决方案是:

 json_decode($points,true);
然后我可以像这样访问我想要的数据阵列:

$results["result"][0]["/location/location/contains"];

你能告诉我们你得到的错误吗?它是否只发生在/en/london作为$fid的情况下,还是发生在任何位置ID的情况下?我从freebase获得了一个json格式的正确答案,例如(有限制){“code”:/api/status/ok”,“result”:[{“/location/location/contains”:[{”/location/location/geocate/latitude”:[{“/location/geocate/latitude”:51.5332999997,/location/geocate/longitude”:-0.1666999999999}],“id:“/en/london\u borough\u of_camden”,“name:“london borough of_camden”}],“id:“/en/london”}],“status:“200 OK”,“transaction\u id:“cache;cache03.p01.sjc1:8101;2011-12-27T17:04:01Z;0047”}但当我将其作为数组时,无法获取“/location/location/contains”中位置的id在json_decode上发现错误(需要有一个true),解决方案是json_decode($points,true);我想要的数据数组是$results[“result”][0][“/location/location/contains”];也许你可以输入这个解决方案作为答案并接受它,这样这个问题就不再显示为“未回答”。它需要有“真”:嗯,那不是真的。您已经解决了这个问题,但它应该像您首先遇到的一样,使用
->
符号。我看到的原始代码的问题是变量名$POISARY中的拼写错误。