skagarwal/google-places-api
Composer 安装命令:
composer require skagarwal/google-places-api
包简介
Google Places Api
README 文档
README
Google Places API for PHP
A PHP wrapper for Google Places API Web Service, compatible with Laravel.
Version Compatibility
| Package Version | PHP | Laravel | Google Places API |
|---|---|---|---|
| ^3.0 | ^8.1 | ^10 | ^11 | Places API and Places API (New) |
| ^2.2 | ^8.0.2 | ^9 | ^10 | ^11 | Places API |
Caution
Version 3 is a complete rewrite using Saloon with breaking changes.
v2.2 API is deprecated and will be removed in next major version. Which means it is backward compatible.
And it is recommended to shift to v3 API before next major release.
You can refer to v2.2 documentation here.
Installation
Install it with composer
composer require skagarwal/google-places-api
General Usage
Laravel user can see the Laravel Usage section
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces; public function foo() { $response = GooglePlaces::make(key: 'API KEY', verifySSL: false, throwOnError: false)->autocomplete('some input'); $data = $response->array(); }
You can also set the API KEY after initiating the class using
GooglePlaces::make()->setKey('KEY')method.
Laravel
Step 1
publish the config file with following artisan command
php artisan vendor:publish --provider="SKAgarwal\GoogleApi\ServiceProvider"
This will create google.php file in the config directory.
Set the API KEY in this config file.
Step 2
Start making requests
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces; public function foo() { $response = GooglePlaces::make()->autocomplete('some input'); $data = $response->collect(); }
Response
The response returned is a Saloon's Response thus you can use all the methods provided by Saloon.
$response->array(); // returns the response as array $response->collect(); // returns the response as collection $response->json(); // returns the response as json $response->status(); // returns the status of the response $response->headers(); // returns the headers of the response $response->body(); // returns the body of the response $response->throw(); // throws an exception if the response is not successful
You can refer to Saloon's documentation for more methods.
Important
By default, no exception is thrown for API errors. You can check the request status with $response->status().
You can also use GooglePlaces::make()->findPlace()->throw() to throw an exception if the request fails.
API Reference
This library supports both the Places API (original) and the Places API (New).
Places API
This section covers methods available for original Places API.
use SKAgarwal\GoogleApi\Places\GooglePlaces; // Original Places API class public function foo() { $response = GooglePlaces::make()->nearbySearch('40.748817,-73.985428'); $data = $response->array(); }
Place Search
findPlace(string $input, string $inputType, array $params = [])
- $input: Text to search (e.g., name, address).
- $inputType:
textqueryorphonenumber. - $params: Optional parameters. More info.
nearbySearch(string $location, ?string $radius = null, array $params = [])
- $location: Latitude,Longitude coordinates. Order - (lat,lng) (e.g.,
40.748817,-73.985428). - $radius: Distance in meters (max 50,000). Required unless using
rankby=distance. - $params: Optional parameters (e.g.,
keyword,type). More info.
textSearch(string $query, array $params = [])
- $query: Search string (e.g.,
"restaurant"). - $params: Optional parameters. More info.
Place Details
placeDetails(string $placeId, array $params = [])
- $placeId: Unique identifier for a place.
- $params: Optional parameters. More info.
Place Autocomplete
placeAutocomplete(string $input, array $params = [])
- $input: Text to search (e.g., name, address).
- $params: Optional parameters. More info.
Query Autocomplete
queryAutocomplete(string $input, array $params = [])
- $input: Text to search (e.g., name, address).
- $params: Optional parameters. More info.
Place Photo
photo(string $photoReference, array $params = [])
Places API (New)
This section covers methods available for Places API (New).
use SKAgarwal\GoogleApi\PlacesNew\GooglePlaces; // New Places API class public function foo() { $response = GooglePlaces::make()->nearbySearch(40.748817, -73.985428, 500.0); $data = $response->array(); }
Autocomplete
autocomplete(string $input, bool $includeQueryPredictions = false, ?array $fields = null, array $params = [])
- $input: Text to search (e.g., name, address).
- $fields: Fields to return. More info.
- $includeQueryPredictions: If
true, the response includes both place and query predictions. The default value is false, meaning the response only includes place predictions. - $params: Optional parameters. More info.
Nearby Search
nearbySearch(float $latitude, float $longitude, float $radius = 0.0, array $fields = ['*'], array $params = [])
- $latitude: Latitude of the location.
- $longitude: Longitude of the location.
- $radius: The radius must be between 0.0 and 50000.0, inclusive. The default radius is 0.0. You must set it in your request to a value greater than 0.0.
- $fields: Fields to return. Default is all fields. More info.
- $params: Optional parameters. More info.
Place Details
placeDetails(string $placeId, array $fields = ['*'], array $params = [])
- $placeId: Unique identifier for a place.
- $fields: Fields to return. Default is all fields. More info.
- $params: Optional parameters. More info.
Text Search
textSearch(string $textQuery, array $fields = ['*'], array $params = [])
- $textQuery: Search string (e.g.,
"restaurant"). - $fields: Fields to return. Default is all fields. More info.
- $params: Optional parameters. More info.
Place Photo
placePhoto(string $name, int $maxHeightPx = null, int $maxWidthPx = null)
- $name: A string identifier that uniquely identifies a photo. More Info
- $maxHeightPx: The maximum desired height of the image in pixels. (Should be between 1 and 4800)
- $maxWidthPx: The maximum desired width of the image in pixels. (Should be between 1 and 4800)
skipHttpRedirect is set to false internally to get JSON response. This cannot be changed
Custom Headers
Set Custom Headers
GooglePlaces::make()->headers()->add('Header-Key', 'Header-Value');
Additional Methods
setKey(string $key): Set the API key.getKey(string $key): Get the API key being used.verifySSL(bool $verifySSL = true): Enable/disable SSL verification.throwOnErrors(bool $throwOnError):- By default, no exception is thrown for API errors. You can check the request status with
$response->status(). - When
throwOnErroris set totrue, the library will throw exceptions on API failures.
- By default, no exception is thrown for API errors. You can check the request status with
Contribution
- Report issues or contribute to the
developbranch. - Open issues/PRs to improve this documentation.
License
This package is licensed under the MIT License.
skagarwal/google-places-api 适用场景与选型建议
skagarwal/google-places-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.25M 次下载、GitHub Stars 达 191, 最近一次更新时间为 2016 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「google」 「places」 「google api」 「google places」 「Google Places Api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 skagarwal/google-places-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 skagarwal/google-places-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 skagarwal/google-places-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Google integration settings and helpers for Laravel Enso
A PSR-7 compatible library for making CRUD API endpoints
Google Maps PHP SDK.
Modern Google Places API client for PHP
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 3.25M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 198
- 点击次数: 7
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-08