public function location($query = null) { if (null !== $query) { $this->setQuery($query); } if (null === $this->_query) { throw new Fizzy_Geocode_Exception('No query passed'); } $parameters = array('appid' => $this->_apiKey, 'location' => $this->_query, 'output' => $this->_output); $uri = Zend_Uri::factory($this->_apiUrl); $uri->setQuery($parameters); if (null === $this->_httpClient) { $this->_httpClient = new Zend_Http_Client($uri); } else { $this->_httpClient->setUri($uri); } $httpResponse = $this->_httpClient->request(); $response = new Fizzy_Geocode_Response(); if (200 != $httpResponse->getStatus()) { if (400 == $httpResponse->getStatus()) { $response->setErrors(array('Bad request')); } elseif (403 == $httpResponse->getStatus()) { $response->setErrors(array('Forbidden')); } elseif (503 == $httpResponse->getStatus()) { $response->setErrors(array('Service unavailable')); } return $response; } $php = unserialize($httpResponse->getBody()); var_dump($php); foreach ($php['ResultSet'] as $result) { $location = new Fizzy_Geocode_Location(); $location->setAddress($result['Address']); $location->setZipcode($result['Zip']); $location->setCity($result['City']); $location->setCountry($result['Country']); $location->setLat($result['Latitude']); $location->setLng($result['Longitude']); $response->addLocation($location); } return $response; }
public function location($query = null) { if (null !== $query) { $this->setQuery($query); } // Build the request parameters $parameters = array('address' => $this->_query, 'sensor' => $this->_sensor ? 'true' : 'false'); if (null !== $this->_countryCode) { $parameters['lg'] = $this->_countryCode; } // Build the request URI $uri = Zend_Uri::factory($this->_apiUrl . $this->_output); $uri->setQuery($parameters); // Send the request if ($this->_client === null) { $this->_client = new Zend_Http_Client($uri); } else { $this->_client->setUri($uri); } $httpResponse = $this->_client->request(); $json = $httpResponse->getBody(); // parse the response into a Fizzy_Geocode_Response $response = new Fizzy_Geocode_Response(); $responseArray = Zend_Json::decode($json); if ($responseArray['status'] != 'OK') { $response->setErrors(array($responseArray['status'])); } foreach ($responseArray['results'] as $result) { $location = new Fizzy_Geocode_Location(); $location->setLat($result['geometry']['location']['lat']); $location->setLng($result['geometry']['location']['lng']); $address = array('number' => null, 'street' => null, 'city' => null, 'zipcode' => null, 'country' => null); foreach ($result['address_components'] as $component) { if (in_array('street_number', $component['types'])) { $address['number'] = $component['long_name']; } if (in_array('route', $component['types'])) { $address['street'] = $component['long_name']; } if (in_array('locality', $component['types'])) { $address['city'] = $component['long_name']; } if (in_array('country', $component['types'])) { $address['country'] = $component['long_name']; } if (in_array('postal_code', $component['types'])) { $address['zipcode'] = $component['long_name']; } } $location->setAddress($address['street'] . ' ' . $address['number']); $location->setZipcode($address['zipcode']); $location->setCity($address['city']); $location->setCountry($address['country']); $response->addLocation($location); } return $response; }