Skip to content

Instantly share code, notes, and snippets.

@redilinxa
Forked from edyrkaj/MapService.php
Created May 9, 2017 14:20
Show Gist options
  • Save redilinxa/2a3128b6ee92dd7f30e6eddbfe486b86 to your computer and use it in GitHub Desktop.
Save redilinxa/2a3128b6ee92dd7f30e6eddbfe486b86 to your computer and use it in GitHub Desktop.
Google Maps API Curl Call - Laravel Service
<?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