Skip to content

Instantly share code, notes, and snippets.

@jamesoates88
Last active December 19, 2015 15:19
Show Gist options
  • Select an option

  • Save jamesoates88/5975755 to your computer and use it in GitHub Desktop.

Select an option

Save jamesoates88/5975755 to your computer and use it in GitHub Desktop.
Get google map directions via PHP using cURL
function super_json_decode($json, &$value, $assoc = false, $depth = 512, $options = 0) {
$pValue = false;
$result = json_decode($json, $assoc, $depth);
if(json_last_error() == JSON_ERROR_NONE) {
$pValue = $result;
return true;
}
return false;
}
function get_google_direction($origin, $destination) {
$ch = curl_init('http://maps.googleapis.com/maps/api/directions/json?origin='.$origin.'&destination='.$destination.'&sensor=false');
curl_setopt_array($ch, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
));
$response = false;
if(super_json_decode(curl_exec($ch), $data)) {
$response = $data;
}
$info = curl_getinfo($ch);
if($data['info']['http_code'] !== 200) {
return false;
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment