PHP Classes

File: http.php

Recommend this page to a friend!
  Classes of Andi P. Trix  >  PHP Geocode Address  >  http.php  >  Download  
File: http.php
Role: Class source
Content type: text/plain
Description: main class
Class: PHP Geocode Address
Get the location of address using Google Maps API
Author: By
Last change: added namespace
Date: 8 years ago
Size: 902 bytes
 

Contents

Class file image Download
<?php
namespace your\namespace\here;

use
Zend\Http\Client;
use
Zend\Http\Client\Adapter\Exception\RuntimeException as ClientRuntimeException;

class
Http {
    protected static
$url = 'http://maps.google.com/maps/api/geocode/json?address=%s&sensor=false';

    public static function
fetchRemoteJson($address) {
        try {
           
$url = sprintf(self::$url, urlencode($address));
           
$client = new Client($url);
           
$json = json_decode($client->send()->getBody());
           
$result = (false == $json) ? array('results' => array(), 'status' => 'JSON_ERROR') : $json;
        } catch (
ClientRuntimeException $e) {
           
$result = array('results' => array(), 'status' => 'HTTP_CLIENT_ERROR');
        } catch (\
Exception $e) {
           
$result = array('results' => array(), 'status' => 'UNKNOWN_ERROR');
        }

        return
$result;
    }
}