public static function Geocode($address, $city, $state, $postal_code, $geocode = null) { logtrace("Geocode", [$address, $city, $state, $postal_code, $geocode]); if (strlen($geocode) > 0) { $latlng = urlencode(str_replace(' ', '', $geocode)); $geocodeURL = signUrl("http://maps.googleapis.com/maps/api/geocode/json?latlng={$latlng}&sensor=false", GOOGLE_MAPS_API_KEY); } else { $address = urlencode($address . ', ' . $city . ', ' . $state . ', ' . $postal_code); $geocodeURL = signUrl("http://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false", GOOGLE_MAPS_API_KEY); } $ch = curl_init($geocodeURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $details = []; logtrace("Geocode: Processing.", null); if ($httpCode == 200) { $geocode = json_decode($result); logtrace("Geocode: Processing.", $geocode); $lat = $geocode->results[0]->geometry->location->lat; $lng = $geocode->results[0]->geometry->location->lng; $formatted_address = $geocode->results[0]->formatted_address; $details['Address'] = $formatted_address; $geo_status = $geocode->status; $location_type = $geocode->results[0]->geometry->location_type; $details['Geocode'] = $result; $details['Location'] = json_encode($geocode->results[0]->geometry); if (is_array($geocode->results[0]->address_components)) { foreach ($geocode->results[0]->address_components as $k => $component) { switch ($component->types[0]) { case 'locality': if ($component->types[1] == 'political') { $details['City'] = $component->long_name; } break; case 'administrative_area_level_1': if ($component->types[1] == 'political') { $details['Province'] = $component->long_name; } break; case 'postal_code': $details['PostalCode'] = $component->long_name; break; } } } logtrace("Geocode: Details.", $details); return $details; } else { logtrace("Geocode: failed.", []); return false; } }
<?php $myUrl = $_POST['url-to-sign']; $baseUrl = $_POST['base-url']; $cryptoKey = "<YOUR_KEY_HERE>"; $hashedResult = hash_hmac('sha1', $myUrl, $decodedKey); echo signUrl($myUrl, $cryptoKey); // Encode a string to URL-safe base64 function encodeBase64UrlSafe($value) { return str_replace(array('+', '/'), array('-', '_'), base64_encode($value)); } // Decode a string from URL-safe base64 function decodeBase64UrlSafe($value) { return base64_decode(str_replace(array('-', '_'), array('+', '/'), $value)); } function signUrl($myUrlToSign, $privateKey) { // parse the url $url = parse_url($myUrlToSign); $urlPartToSign = $url['path'] . "?" . $url['query']; // Decode the private key into its binary format $decodedKey = decodeBase64UrlSafe($privateKey); // Create a signature using the private key and the URL-encoded // string using HMAC SHA1. This signature will be binary. $signature = hash_hmac("sha1", $urlPartToSign, $decodedKey, true); $encodedSignature = encodeBase64UrlSafe($signature); return $myUrlToSign . "&signature=" . $encodedSignature; }
$http_host = implode('/', array_slice($arr1, 0, 3)); header('Content-Type: text/plain; charset=utf-8'); header('Access-Control-Allow-Origin: ' . $http_host); header('Access-Control-Allow-Credentials: true'); // Encode a string to URL-safe base64 function encodeBase64UrlSafe($value) { return str_replace(array('+', '/'), array('-', '_'), base64_encode($value)); } // Decode a string from URL-safe base64 function decodeBase64UrlSafe($value) { return base64_decode(str_replace(array('-', '_'), array('+', '/'), $value)); } // Sign a URL with a given crypto key // Note that this URL must be properly URL-encoded function signUrl($myUrlToSign, $privateKey) { // parse the url $url = parse_url($myUrlToSign); $urlPartToSign = $url['path'] . "?" . $url['query']; // Decode the private key into its binary format $decodedKey = decodeBase64UrlSafe($privateKey); // Create a signature using the private key and the URL-encoded // string using HMAC SHA1. This signature will be binary. $signature = hash_hmac("sha1", $urlPartToSign, $decodedKey, true); $encodedSignature = encodeBase64UrlSafe($signature); return $myUrlToSign . "&signature=" . $encodedSignature; } print signUrl($uri, $crypto);