google maps api foreach and echoing nearest subway station names

时间:2018-03-09 19:13:15

标签: php

I am trying to get the contents "name" of the nearest subway stations via the google maps api.

But i'm getting no results for some reason.

any ideas?

<?
$stations = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=XXXXX&location=51.517238,-0.0731695&rankby=distance&type=subway_station');
$stations = json_decode($stations);

foreach ($stations as $data)
    {


echo  $data->results->name;

    }?>

**PRINT / VARDUMP OF $STATIONS **

object(stdClass)#113 (4) { ["html_attributions"]=> array(0) { } ["next_page_token"]=> string(468) "CrQCIQEAAIiUu7iBF_FuePInfUCwzG4ycBN9KTs3IQd0TF_VvYoz3ZHslqgGYANYyWPPcrtyLFjnHi6iBgbJqDTl972-fOLJsCJRhyKZsrXvx7I6RG9XBGFo4iVMqh10MVVjkCWSXKyW1M-vGneha2KLzQogqiGgYBLkG1dhyzlnaSmmpC0ur6rlzyTIRKQSewB79OjXi__cq7YuIcexmA3FqZHUqGtj2c8GL6X76wTeZWD37Rsl2WxC9FAsIqHQvxfOAySbf0xXtyWq6lxdBUt7rzbVRYfn5SrfQOapJGTe9Tx-ZculyJyMYEEC4pm4noVghQy0_LNgYCUxaiLjQv5_2DB4y5CPi8tDbLjAcHC7YOtAYEDYeIR7N5LAg2LDrXe378Z5zr0q8iLYmXqCFXoCJ9kwo4ISEFJHIBPTmrBDlEk9iCH8oEUaFBnA2CBg9vn9uYcNGZfFAQ50qyHk" ["results"]=> array(20) { [0]=> object(stdClass)#115 (11) { ["geometry"]=> object(stdClass)#116 (2) { ["location"]=> object(stdClass)#117 (2) { ["lat"]=> float(51.5151582) ["lng"]=> float(-0.0719412) } ["viewport"]=> object(stdClass)#118 (2) { ["northeast"]=> object(stdClass)#119 (2) { ["lat"]=> float(51.516427280292) ["lng"]=> float(-0.070816719708498) } ["southwest"]=> object(stdClass)#120 (2) { ["lat"]=> float(51.513729319709) ["lng"]=> float(-0.073514680291502) } } } ["icon"]=> string(73) "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png" ["id"]=> string(40) "47152d423eb61361555b2d8908e75c59d79deb1b" ["name"]=> string(12) "Aldgate East" ["photos"]=> array(1) { [0]=> object(stdClass)#121 (4) { ["height"]=> int(3966) ["html_attributions"]=> array(1) { [0]=> string(97) "Rene Lichtenstein" } ["photo_reference"]=> string(190) "CmRaAAAAPfCFksoZGq8FY3uKNCa271MIxH8dP7ZsE1lPnsvMep2OatQxY1SRN8LWxL4lRE_66vldeQbXnX7nrWg_bNLsGk9MYokXXq16ov1Hu01CSQe2nCqIQm6UzwcmjcqskSsDEhAIWrLuwREjELzxSkcPETQuGhSxU2Tai6pf8v4ugWTFDDz0amDAWA" ["width"]=> int(5950) } } ["place_id"]=> string(27) "ChIJ_7Q5M7UcdkgRCv-7nswC520" ["rating"]=> float(3.9) ["reference"]=> string(190) "CmRRAAAAqjpKiEO85qzXCVhdy9AhIyHZ2smVOnoDyGndzuW_s8G6yniXFcqGkdDTSA6xKZgLznbJUFbYRU3Kdth_wcx9aQXyvH7iua_zdt3PQ_Q-I3HstwRpW1nXDGXLFwcU6LVjEhD8CFwbm_VmLIpv820hobt3GhSQ9hZsbbGIoQ81bwfxTNHkMTKA6w" ["scope"]=> string(6) "GOOGLE" ["types"]=> array(4) { [0]=> string(14) "subway_station" [1]=> string(15) "transit_station" [2]=> string(17) "point_of_interest" [3]=> string(13) "establishment" } ["vicinity"]=> string(27) "Whitechapel High St, London" } [1]=> object(stdClass)#122 (11) { ["geometry"]=> object(stdClass)#123 (2) { ["location"]=> object(stdClass)#124 (2) { ["lat"]=> float(51.5142301) ["lng"]=> float(-0.0755928) } ["viewport"]=> object(stdClass)#125 (2) { ["northeast"]=> object(stdClass)#126 (2) { ["lat"]=> float(51.515579080292) ["lng"]=> float(-0.074243819708498) } ["southwest"]=> object(stdClass)#127 (2) { ["lat"]=> float(51.512881119709) ["lng"]=> float(-0.076941780291502) } } } ["icon"]=> string(73) "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"  

1 个答案:

答案 0 :(得分:0)

正在运行print_r($stations);会返回错误:

stdClass Object ( [error_message] => The provided API key is invalid. [html_attributions] => Array ( ) [results] => Array ( ) [status] => REQUEST_DENIED )

看起来之前已经看过这个问题了,但是没有按照我所做的方式在不打印电台的情况下回答问题。 以下是指向Google所需的商家帐户的答案链接:

google maps api, The provided API key is invalid

另外,参考Google文档,您的网址可能格式不正确:

https://developers.google.com/maps/documentation/javascript/error-messages

如果有帮助,请告诉我。

修改

这是另一个可能有用的答案:

Google Places API Web Services API Key Invalid

您可以替换`key = XXXXX&#39;使用按照https://developers.google.com/places/web-service/get-api-key

上的说明生成的密钥

修改

这是要解析的数据的日志,但我认为$data->results数组应该是您正在迭代的数组。您是否尝试将上下文更改为:

<?php
$stations = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=XXXXX&location=51.517238,-0.0731695&rankby=distance&type=subway_station');
$stations = json_decode($stations);
$stations = $stations->results;
foreach ($stations as $data)
    {

echo  $data->name;

    }

老实说,拥有API密钥的人可能会更好地回答这个问题,但这是我最好的猜测,没有可解析格式的数据可以重读。不是说可以做到,只需要一个很多工作,我认为这应该是我读取var转储的方式。

最终编辑

我可以确认以上代码有效。在for循环中使用echo <br>;打印时的结果将导致:

Aldgate East
Aldgate Station
Liverpool Street
Tower Hill
Whitechapel
Moorgate Station
Monument Station
Bank Underground Station
Old Street
Old Street
Cannon Street Underground Station
Mansion House Underground Station
London Bridge Underground Station
Bethnal Green
St. Paul's
Barbican Station
Stepney Green Underground Station
Blackfriars
Farringdon
Bermondsey