Php 使用foreach遍历JSON

Php 使用foreach遍历JSON,php,json,foreach,google-places-api,geocoding,Php,Json,Foreach,Google Places Api,Geocoding,我一直遵循本指南使用两个不同的API创建应用程序,但本指南很旧,因此其中一个API的工作方式与指南中的不同。我正试图从谷歌地理编码API中获取坐标,并将它们粘贴到网站的位置。我是PHP新手,所以我按照指南中的示例遍历JSON对象,但为了让它正常工作,我整个晚上都被卡住了。 下面是PlaceSearchAPI中的JSON对象 { "html_attributions":[ ], "results":[ { "geometry":{ }, "icon":"ht

我一直遵循本指南使用两个不同的API创建应用程序,但本指南很旧,因此其中一个API的工作方式与指南中的不同。我正试图从谷歌地理编码API中获取坐标,并将它们粘贴到网站的位置。我是PHP新手,所以我按照指南中的示例遍历JSON对象,但为了让它正常工作,我整个晚上都被卡住了。 下面是PlaceSearchAPI中的JSON对象

{  
"html_attributions":[  ],
"results":[  
  {  
     "geometry":{  },
     "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
     "id":"d4b0fb0f7bf5b2ea7df896a0c120a68efae039cf",
     "name":"Guadalajara Mexican Grill & Cantina",
     "opening_hours":{  },
     "photos":[  
        {  
           "height":2952,
           "html_attributions":[  ],
           "photo_reference":"CmRaAAAAfO4JKUaO8vCFM2dcu5LMu4mA4_HXQGJ1FyAnyJUre_kD6VOWiQj7tBEECx4AAct5AORIKipSYWg-Zprjlf8o-SFd7mBRGMXMVMwodFZ5KMLwPYPUhBnTTehGPkb9275pEhCkAqMwfmK29vYenk1wdwFvGhSIHR8ch6FONc99tGn4rVnesbuteg",
           "width":5248
        }
     ],
     "place_id":"ChIJ27es4SWa3IARcvjmt3xL2Aw",
     "price_level":2,
     "rating":4.4,
     "reference":"CmRRAAAA7Rx-l7juDX-1or5dfpK6qFcZ0trZ9cUNEUtKP2ziqHb2MhOE6egs-msJ2OdFKEuHhuNe-3Yk6yxUYwxCBqhDT3ci8pYZI4xYhPGyyDgDenbEU_8k84JiEtCGvj4bdIR0EhDR2Pqte5_kDUcCC9PJFVknGhQomvD4d7NBIhCFxI4i2iEc0w9UiA",
     "scope":"GOOGLE",
     "types":[  ],
     "vicinity":"105 North Main Street, Lake Elsinore"
  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  },
  {  }
],
"status":"OK"
}
我正在尝试将所有照片引用捕获到一个数组中,然后将它们插入google的Place Photos API。以下是我的尝试:

更新

这里是什么?
键入一个位置
该程序将显示该区域内要去的地方的图片

走!


似乎无法让foreach循环执行任何操作变量或API调用是第一个问题,而foreach循环是第二个问题

json_解码($someJSON,true)
从json创建关联数组,因此不能使用
->
箭头,这些箭头用于对象。 没有
$item->photo\u参考
,请使用:

$results = $places_array["results"];

foreach ($results as $item) {
    echo $item["photos"]["photo_reference"];
}

$lat、$lng
变量或API调用是第一个问题,而foreach循环是第二个问题

json_解码($someJSON,true)
从json创建关联数组,因此不能使用
->
箭头,这些箭头用于对象。 没有
$item->photo\u参考
,请使用:

$results = $places_array["results"];

foreach ($results as $item) {
    echo $item["photos"]["photo_reference"];
}

无效的请求意味着错误的url或错误的参数 如果$lat和$lng是变量,那么插值将无法使用单引号,请尝试使用如下双引号 “地点=$lat,$lng”

您应该删除半径或距离,因为您无法在文档上同时获得半径或距离

这是我在localhost上修改过的代码,请注意$contextOptions,您不应该在代码上复制它。这是一个解决方法,可以使文件内容在我的机器上工作

之后,foreach应该很容易,因为它只是一个数组,可以查看代码

 $thelocation = "1600+Amphitheatre+Parkway,+Mountain+View,+CA";
$thekey = "someapikey";
$maps_url = 'https://' .
        'maps.googleapis.com/' .
        'maps/api/geocode/json' .
        '?address=' . urlencode($thelocation) .
        '&key=' . $thekey;

$contextOptions = array(
 "ssl" => array(
 "verify_peer"      => false,
 "verify_peer_name" => false,
 ),
);

$maps_json = file_get_contents($maps_url, 0, stream_context_create($contextOptions));// file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);

$lat = $maps_array['results'][0]['geometry']['location']['lat'];
$lng = $maps_array['results'][0]['geometry']['location']['lng'];

$places_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' .
        "location=$lat,$lng" .
        '&rankby=distance' .
        '&key='.$thekey;
//https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&key=
$places_json = file_get_contents($places_url,0, stream_context_create($contextOptions));
$places_array = json_decode($places_json, true);

if (!empty($places_array)) {


 foreach ($places_array["results"] as $item) {

        echo $item["name"]."<br>";

    }
}

无效的请求意味着错误的url或错误的参数 如果$lat和$lng是变量,那么插值将无法使用单引号,请尝试使用如下双引号 “地点=$lat,$lng”

您应该删除半径或距离,因为您无法在文档上同时获得半径或距离

这是我在localhost上修改过的代码,请注意$contextOptions,您不应该在代码上复制它。这是一个解决方法,可以使文件内容在我的机器上工作

之后,foreach应该很容易,因为它只是一个数组,可以查看代码

 $thelocation = "1600+Amphitheatre+Parkway,+Mountain+View,+CA";
$thekey = "someapikey";
$maps_url = 'https://' .
        'maps.googleapis.com/' .
        'maps/api/geocode/json' .
        '?address=' . urlencode($thelocation) .
        '&key=' . $thekey;

$contextOptions = array(
 "ssl" => array(
 "verify_peer"      => false,
 "verify_peer_name" => false,
 ),
);

$maps_json = file_get_contents($maps_url, 0, stream_context_create($contextOptions));// file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);

$lat = $maps_array['results'][0]['geometry']['location']['lat'];
$lng = $maps_array['results'][0]['geometry']['location']['lng'];

$places_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' .
        "location=$lat,$lng" .
        '&rankby=distance' .
        '&key='.$thekey;
//https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&key=
$places_json = file_get_contents($places_url,0, stream_context_create($contextOptions));
$places_array = json_decode($places_json, true);

if (!empty($places_array)) {


 foreach ($places_array["results"] as $item) {

        echo $item["name"]."<br>";

    }
}


var_dump($places_array)打印什么吗?数组(3){[“html_attributes”]=>array(0){}[“results”]=>array(0){}[“status”]=>string(15)“INVALID_REQUEST”}数组(3){[“html_attributes”]=>array(0){}[“results”]=>array(0){}[“status”]=>string(15)“INVALID_REQUEST”}数组(3){[“html_attributes”]=>array(0){}]=>array(0){}[“status”]=>string(15)“无效的请求”}我已经更新了我的answerwell检查我的answerdoes var_dump($places_array)打印了什么?array(3){[“html_属性”]=>array(0){}[“results”]=>array(0){}[“status”]=>string(15)“无效的请求”}array(3){[“html_属性”=>array(0){}[“results”=>array(0){}[“状态”]=>string(15)“无效的_请求”}数组(3){[“html_属性”]=>array(0){}[“结果”]=>array(0){}[“状态”]=>string(15)“无效的_请求”}我已经更新了我的应答器。请检查我的应答器。它没有抛出任何错误,但它仍然没有出现在我的屏幕上的任何地方。它已经在那里了。或者你的确切意思是什么?我有$places\u array=json\u decode($places\u json,true);就在你上面对var\u dump($places\u array)的评论中的if语句之前可能$lat、$lng变量没有在$places\url中定义。我想如果您的变量中没有正确的json,foreach将不会产生任何结果。您确定您从url中获得了任何数据吗?它没有抛出任何错误,但仍然没有在我的屏幕上弹出。它已经在那里了。或者您的确切意思是什么?我有$places\u array=json\u decode($places\u json,true);就在您上面对var\u dump($places\u array)的评论中的if语句之前可能$lat、$lng变量未在$places\u url中定义。如果变量中未填充正确的json,我认为foreach不会产生任何结果。您确定您从url中获得了任何数据吗?我仍然从var\u dump($places\u array)收到无效请求请尝试将该url与所有值一起放在您的浏览器上,并确保您的api密钥未过期。我更新了我的密钥,所以现在没问题,但我仍然收到来自该网站的无效请求,因此这意味着该url是错误的。虽然不确定在哪里。我现在获得了该url,但代码仍然不确定从何处开始。如果url工作,然后请使用这些参数和值创建一个新的var\u转储($places\u数组)。我仍然从var\u转储($places\u数组)获得一个无效请求请尝试将该url与所有值一起放在您的浏览器上,并确保您的api密钥未过期。我更新了我的密钥,所以现在没问题,但我仍然收到来自该网站的无效请求,因此这意味着该url是错误的。虽然不确定在哪里。我现在获得了该url,但代码仍然不确定从何处开始。如果url工作,然后请使用这些参数和值创建一个新的var\u dump($places\u array)
AVEonline.co
KRAV MAGA GLOBAL WORLD MAP
Mark Carvalho
Amyan
Moving the Planet
Sosta in Camper
NosCode
GLOBAL BUZZ
OptiClean
JI-SU TELECOM
Reel Thrillz
Clío Reconstrucción Histórica
AprimTek
Hayjayshop
NHAV
gitanos.cat
Being Digitall
Directory+
AdExperts
Optical Spectroscopy and Nanomaterials Group