PHP Geocoding Quick Start Guide - QuotaGuard

    Table of contents

    Installation

    When you sign up with QuotaGuard on Heroku, you will be provided with a unique username and password that you can use when configuring your proxy service in your application:

    http://username:password@proxy.quotaguard.com:9292
    

    We recommend you store the connection string in an environment variable QUOTAGUARD_URL to maintain compatibility with platforms like Heroku.

    You can test your proxy setup using curl:

    $ curl -x username:password@proxy.quotaguard.com:9292 http://www.google.com/
    

    Integration

    PHP cURL is the easiest way to make HTTP requests via a proxy. The below shows an example of making a geocoding request:

    <?php
    
    function lookup($string){
      $quotaguard_env = getenv("QUOTAGUARD_URL"); 
      $quotaguard = parse_url($quotaguard_env);
    
      $proxyUrl = $quotaguard['host'].":".$quotaguard['port'];
      $proxyAuth = $quotaguard['user'].":".$quotaguard['pass']; 
     
       $string = str_replace (" ", "+", urlencode($string));
       $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";
     
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $details_url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_PROXY, $proxyUrl);
       curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
       curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
       $response = json_decode(curl_exec($ch), true);
     
       // If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
       if ($response['status'] != 'OK') {
        return null;
       }
     
       print_r($response);
       $geometry = $response['results'][0]['geometry'];
     
        $longitude = $geometry['location']['lng'];
        $latitude = $geometry['location']['lat'];
     
        $array = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
            'location_type' => $geometry['location_type'],
        );
     
        return $array;
     
    }
     
    $city = 'San Francisco, USA';
     
    $array = lookup($city);
    print_r($array);
     
    ?>
    

    Ready to Get Started?

    Get in touch or create a free trial account