Json_decode Google映射api奇怪的输出

I am trying to get information by json_decode from Google maps API, but for some reason I get a strange output. Can someone help me with this?

$request = file_get_contents ('https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true');
$data = json_decode( $request, true );


foreach ( $data['results'][0]['geometry'] as $value ) {
    echo $value['lat'] . '<br />';
    echo $value['lng'] . '<br />';
}

This is my output:

51.6303446
4.8655305
R
R

It looks like the script takes the first letter of:

"location_type" : "ROOFTOP",

what comes next if you watch this: https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true

I don't need the 2 R's.. Does anyone know what I am doing wrong?

EDIT: var_dump($data['results'][0]['geometry'])

Array
(
[location] => Array
    (
        [lat] => 51.6303446
        [lng] => 4.8655305
    )

[location_type] => ROOFTOP
[viewport] => Array
    (
        [northeast] => Array
            (
                [lat] => 51.631693580291
                [lng] => 4.8668794802915
            )

        [southwest] => Array
            (
                [lat] => 51.628995619708
                [lng] => 4.8641815197085
            )

    )
)

Late to the party, but I wanted to add what has worked for me as I searched high and low early on with Google Maps and how to dynamically collect lat and long much the way the OP has described. Please correct me if I am incorrect as I am still learning PHP and Google Maps in general, but again, this code works for my needs and I wanted to share this with the OP and others that may be searching for examples of working code that fits the OPs criteria.

I created this function after much banging of head to keyboard, this works for me in PHP 7. I see no need for a loop if you are collecting one addresses geo location.

  • Simply decode your http request from google api, check the request for validation, then assign the values of the lat and long to variables to be used as needed in your application/project.

I have included an example of how I collect Lat and Long of customers that sign up through a form with a simple street address.

*#NOTE: I have not shown any example of cleaning the posted variables in this answer. Please make sure to clean your posts of unwanted html entities/slashes/empty spaces, etc... *

function get_lat_and_long($address){
    // $address = The address string in a proper address format, we run through urlencode();
    // Get JSON results from this request
    // APIKEY = *Google API personal key* and is a constant coming from an external constants.php file.
    // You can add your key statically though I recommend saving all sensitive variables/constants and arrays in a server side file that has no access by regular users. 
    $geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
    // Convert JSON and validate
    $geo = json_decode($geo, true);
        if ($geo['status'] == 'OK') {
        // Get Lat & Long
        $latitude = $geo['results'][0]['geometry']['location']['lat'];
        $longitude = $geo['results'][0]['geometry']['location']['lng'];
        }else{ 
            $latitude = NULL;
            $longitude = NULL;
        }
    return $latitude;
    return $longitude;
}

Without a function...

//create string holding address to run through google geolocation API for Lat/Long
            $address = $_POST['usr_street'].' '.$_POST['usr_city'].' '.$_POST['usr_state'].', '.$_POST['usr_zip'];

            // Get JSON results from this request
            $geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);

            // Convert JSON 
            $geo = json_decode($geo, true);

            if ($geo['status'] == 'OK') {
              // Get Lat & Long
                $latitude = $geo['results'][0]['geometry']['location']['lat'];
                $longitude = $geo['results'][0]['geometry']['location']['lng'];
            }else{ 
                $latitude = NULL;
                $longitude = NULL;
            }

From here, I can push $latitude and $longitude into my DB array and save the value in my DB column. Check it. EX.

$userData = array(
     'usr_first_name' => $_POST['usr_first_name'],
     'usr_last_name' => $_POST['usr_last_name'],
     'usr_password' => password_hash($_POST['usr_password'], PASSWORD_DEFAULT),
     'usr_email' => $_POST['usr_email'],
     'usr_company_name' => $_POST['usr_company_name'],
     'usr_website' => $_POST['usr_website'],
     'usr_street' => $_POST['usr_street'],
     'usr_city' => $_POST['usr_city'],
     'usr_state' => $_POST['usr_state'],
     'usr_zip' => $_POST['usr_zip'],
     'usr_phone1' => $_POST['usr_phone1'],
     'usr_lat' => $latitude,
     'usr_long' => $longitude,
     //->etc...etc...etc...
 );
     //->$user is user object declared further up in document and comes from external user class -> user.class.php
     //-> insert is function from external USER class that inserts query into DB.
     $insert = $user->insert($userData);

Hope this helps others that are struggling with Google Maps API geo Location with PHP. In terms of dynamically collecting Lat and Long using simple address string constructed from form entries.