Popular just now


Zend Framework and Google Maps API Example

When looking for a Senior PHP Developer position I get a lot of tests. Most of them I kindly refuse to do because they always involve array manipulation and have no real world context. They only show that the interviewer is not aware of what is real world. Sometimes the test is trying to solve problem that cannot be solved. I always ask who that works for them has solved the problem and can I meet them. Rather than babble on about the red flags that the answers to my question bring up I'll show my answer to a test. First because it was in the form of a spec. I like this. Secondly, because it involves using Google Maps API and object oriented PHP and Zend Framework I was happy to oblige. It was a useful real world test and harder than I expected because the Zend Framework v1 has horrible documentation and worse examples(none of them work). Zend Framework v2 has less. So after piecing together something that works I decided to share. This is the code needed to connect to Google Maps API and display the results in JSON using ZF2 as a library.

 * @author Carl McDade
 * @since 2013-05-09
 * @version
 * Use: go the the address bar and enter a place using the format
 * http://berlinto.com/test.php?place=berlin
 * http://berlinto.com/test.php?place=new jersey
 * http://berlinto.com/test.php?place=1 hampton rd. England
 * Berlin, Germany
 $path = __DIR__ .'/path/to/zend/library';
 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
 $loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
 use Zend\Http\Request;
 use Zend\Http\Client;
 class zftest{
 protected $place;
 function __construct()
  $this->place = $_GET['place'];
 function zend_test()
  $uri = 'http://maps.google.com/maps/api/geocode/json';
  $address = urlencode($this->place);
  $sensor = 'false';
  $request = new Request();
  $client = new Client($uri);
  $response = $client->dispatch($request); 
  if ($response->isSuccess())
   header('Content-Type: text/html; charset=utf-8');
   print 'Your Request for:
' . print_r($address, 1) . '
'; print '
' . print_r($response->getBody(), 1) . '
'; } } } $maps = new zftest(); $maps->zend_test();

No comments:

Post a Comment