dimanche 24 juillet 2016

Fetching JSON data using PHP from Google geocode API

The first error I get is "Call to undefined function get_file_contents() on line 18", I've tried to define get_file_contents() in the beginning of the file. The error dissappears, but nothing is displayed from echo $lat;

<?php 

require_once('../db.php');

$api_key = "somekey";

$sqlQuery = mysql_query("SELECT `County` FROM `table`"); 



/* Fetch county */ 

while($rows = mysql_fetch_array($sqlQuery))  { 

    $countyArr = $rows['County']; 



    /* Call google API and save coordinates for each county */ 

    $json = get_file_contents("https://maps.googleapis.com/maps/api/geocode/json?address=".$countyArr.",+CA&key=".$api_key."");

    $obj = json_decode($json);



    if ($obj->status == "OK") {

        $lat = $obj->results->location->lat;
        $lng = $obj->results->location->lng;

        echo $lat;




    }

}

?>

This is the API I'm using: https://developers.google.com/maps/documentation/geocoding/get-api-key

Aucun commentaire:

Enregistrer un commentaire