-
-
Save redilinxa/2a3128b6ee92dd7f30e6eddbfe486b86 to your computer and use it in GitHub Desktop.
Google Maps API Curl Call - Laravel Service
This file contains 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
<?php | |
/** | |
* Copyright (c) 2017. Ezeclip | |
*/ | |
namespace App\Services; | |
class MapService { | |
// Define Constants | |
const GOOGLE_API_KEY = "XXXXX"; | |
/** | |
* @param string $address | |
* | |
* @author Eledi Dyrkaj | |
* @company Manoolia/Digitaleheimat | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public static function getLocation( $address = "" ) { | |
$address = urlencode( $address ); | |
$key = self::GOOGLE_API_KEY; | |
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=$key"; | |
// Create a curl call | |
$ch = curl_init(); | |
$timeout = 5; | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_HEADER, 0 ); | |
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); | |
$data = curl_exec( $ch ); | |
// send request and wait for response | |
$response = json_decode( $data, true ); | |
curl_close( $ch ); | |
return response()->json( $response, 200 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment