Skip to content

Commit

Permalink
Added Place Photo Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin Agarwal committed Feb 21, 2018
2 parents 844e35f + b008809 commit 8c6b459
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![License](https://poser.pugx.org/skagarwal/google-places-api/license?format=flat-square)](https://packagist.org/packages/skagarwal/google-places-api)


# Google Places APi.
# Google Places API.

This is a PHP wrapper for **Google Places Api Web Service**. And is [Laravel Framework](https://laravel.com/docs/5.2) friendly.

Expand All @@ -17,6 +17,7 @@ With just 2 lines of code you can request to any google places api feature. No n
* [Place Autocomplete](#place-autocomplete) This service is Used to automatically fill in the name and/or address of a place as you type.
* [Query Autocomplete](#query-autocomplete) This service is Used to provide a query prediction service for text-based geographic searches, by returning suggested queries as you type.
* [Place Add](#place-add) This service Used to Add/Delete a place to Google's Place database
* [Place Photo](#place-photo) This gives you access to the millions of photos stored in the Google's Places database
* [Additional Methods](#additional-methods) Additional Methods Available.

# Installation
Expand Down Expand Up @@ -162,6 +163,8 @@ _**(This method is depricated, and will be removed when google removes it from t
* `input` — The text string on which to search. The Places service will return candidate matches based on this string and order results based on their perceived relevance.
* `params` - **Optional Parameters** You can refer all the available optional parameters on the [Google's Official Webpage](https://developers.google.com/places/web-service/query)

---

<a name=place-add></a>
# Place Add
### addPlace($params)
Expand All @@ -172,6 +175,13 @@ _**(This method is depricated, and will be removed when google removes it from t
_**(This method is depricated, and will be removed when google removes it from the api)**_
* `placeId` - The Place Id you want to delete.

---

<a name=place-photo></a>
# Place Photo
### photo($photoReference, $params = [])
* `params` - The set of key-value parameters necessary to add a place to Google. You can refer to the fields on [Google's Official Webpage regarding Place Add](https://developers.google.com/places/web-service/photos)

<a name=additional-methods></a>
# Additional Methods
### getStatus()
Expand Down
78 changes: 64 additions & 14 deletions src/PlacesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace SKAgarwal\GoogleApi;

use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use SKAgarwal\GoogleApi\Exceptions\GooglePlacesApiException;

class PlacesApi
{
const BASE_URL = 'https://maps.googleapis.com/maps/api/place/';

const NEARBY_SEARCH_URL = 'nearbysearch/json';

const TEXT_SEARCH_URL = 'textsearch/json';
Expand All @@ -23,6 +26,8 @@ class PlacesApi

const PLACE_DELETE_URL = 'delete/json';

const PLACE_PHOTO_URL = 'photo';

/**
* @var
*/
Expand Down Expand Up @@ -54,7 +59,7 @@ public function __construct($key = null, $verifySSL = true)
$this->key = $key;

$this->client = new Client([
'base_uri' => 'https://maps.googleapis.com/maps/api/place/',
'base_uri' => self::BASE_URL,
]);
}

Expand Down Expand Up @@ -141,6 +146,36 @@ public function placeDetails($placeId, $params = [])
return $this->convertToCollection($response);
}

/**
* @param $photoReference
* @param array $params
*
* @return mixed|string
* @throws \SKAgarwal\GoogleApi\Exceptions\GooglePlacesApiException
*/
public function photo($photoReference, $params = [])
{
$this->checkKey();

$params['photoreference'] = $photoReference;

if (!array_any_keys_exists(['maxwidth', 'maxheight'], $params)) {
throw new GooglePlacesApiException('maxwidth or maxheight param is required');
}

$options = $this->getOptions($params);

$url = '';

$options['on_stats'] = function (TransferStats $stats) use (&$url) {
$url = $stats->getEffectiveUri();
};

$this->client->get(self::PLACE_PHOTO_URL, $options);

return (string) $url;
}

/**
* Place AutoComplete Request to google places api.
*
Expand Down Expand Up @@ -231,19 +266,7 @@ public function deletePlace($placeId)
*/
private function makeRequest($uri, $params, $method = 'get')
{
$options = [
'query' => [
'key' => $this->key,
],
];

if ($method == 'post') {
$options = array_merge(['body' => json_encode($params)], $options);
} else {
$options['query'] = array_merge($options['query'], $params);
}

$options['verify'] = $this->verifySSL;
$options = $this->getOptions($params, $method);

$response = json_decode(
$this->client->$method($uri, $options)->getBody()->getContents(),
Expand Down Expand Up @@ -390,4 +413,31 @@ public function verifySSL($verifySSL = true)

return $this;
}

/**
* @param array $params
* @param string $method
*
* @return array
*/
private function getOptions($params, $method = 'get')
{
$options = [
'query' => [
'key' => $this->key,
],
];

if ($method == 'post') {
$options = array_merge(['body' => json_encode($params)], $options);
} else {
$options['query'] = array_merge($options['query'], $params);
}

$options['http_errors'] = false;

$options['verify'] = $this->verifySSL;

return $options;
}
}

0 comments on commit 8c6b459

Please sign in to comment.