<?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;
}
}
|