0

I am attempting to capture a latitude and longitude and then pass it to Google Maps to return some data and then exact the zip code out of the response and use that.

When I call it directly to a browser I get a response on the page that ends up being like:

{"zip":"90029"}

But when I attempt to view it in Chrome's developer tools, it states "This request has no response data". So it seems as though my JSON is not being encoded properly, or I am not returning it to the page properly in a JSON format type header.

I've tried various things, and nothing seems to work properly in order to get it return the JSON data.

Here's my present code:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$now = @date("Ymd-His");

$myFile = "loc" . $now . ".txt";
$fh = fopen("locations/" . $myFile, 'w') or die("can't open file");

$lat = $_REQUEST['lat'];
$long = $_REQUEST['long'];
$type = $_REQUEST['type'];
$phone = $_REQUEST['phone'];
$loc = $_REQUEST['typed_location'];

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";




$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);

$address = json_decode($curlData, true);

$temp_data = $address['results'][0]['formatted_address'];
$temp = explode(", ", $temp_data);
$temp_2 = explode(" ", $temp[2]);
$zip = $temp_2[1];

fwrite($fh, $zip);
fclose($fh);


$array = array("zip" => $zip);

$t = json_encode($array);
echo $t;
halfer
  • 19,824
  • 17
  • 99
  • 186
MrTechie
  • 1,797
  • 4
  • 20
  • 36
  • I Can't yet comment but try this http://stackoverflow.com/a/3019085/2349963 (It changed to comment automatically) – Rohit Hazra Aug 09 '15 at 20:46
  • `@date("Ymd-His")` Please, please don’t do this! If it tells you to set a time zone then set one. Using the `@` operator is always a big mistake. – idmean Aug 09 '15 at 21:00
  • Thanks, but unfortunately, neither one pertain to my question. – MrTechie Aug 09 '15 at 21:11

1 Answers1

0

For anyone else looking to solve this problem - when using cross domains, be sure to allow for the Access-Control by setting the following as a header:

header("access-control-allow-origin: *");
MrTechie
  • 1,797
  • 4
  • 20
  • 36