Last active
December 19, 2015 15:19
-
-
Save jamesoates88/5975755 to your computer and use it in GitHub Desktop.
Get google map directions via PHP using cURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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