PHP Geocoding Quick Start Guide - XLRoutes

    Installation

    When you sign up with XLRoutes 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.xlroutes.com:9292
    

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

    You can test your proxy setup using curl:

    $ curl -x username:password@proxy.xlroutes.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){
      $xlroutes_env = getenv("xlroutes_URL"); 
      $xlroutes = parse_url($xlroutes_env);
    
      $proxyUrl = $xlroutes['host'].":".$xlroutes['port'];
      $proxyAuth = $xlroutes['user'].":".$xlroutes['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);
     
    ?>