peoplegogo/google-places

Google Places API 的 PHP 封装。

1.0.3 2016-04-03 12:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:05 UTC


README

Latest Stable Version License Build Status

Logo of Google Places API Web Service

这是一个 Google Places API Web 服务的 PHP 封装。

安装

Composer

推荐通过 Composer 进行安装。在您的 composer.json 文件中添加以下内容

{
    "require": {
        "peoplegogo/google-places": "~1.0"
    }
}

或者使用浏览器中的以下命令

composer require peoplegogo/google-places

在 Packagist 中检查 此处

手动安装

步骤

  • 将 src/GooglePlaces.php 复制到您的代码库中,例如到 vendor 目录。
  • 将 GooglePlaces 类添加到您的自动加载器中,或者直接 require 该文件。

入门

首先您需要创建一个 GooglePlaces 实例

$google_places = new GooglePlaces();

您可以在创建实例时传递 API 密钥(用于在 Google 服务面前进行身份验证),或者您可以在之后设置它。

$google_places = new GooglePlaces($apiKey);

或者

$google_places = new GooglePlaces();
$google_places->setApiKey($apiKey);

默认格式设置为 json,但您可以这样更改

$google_places = new GooglePlaces($apiKey, $format);

或者

$google_places->setFormat($format);

如果您想,也可以设置语言。

$google_places->setLanguage($language_code);

这样,就不需要在每个请求中设置语言了。

您还可以准备输入参数

$params = [
	'input' => 'Vict',
	'types' => '(cities)',
	'language' => 'pt_BR' 
];

设置完所有参数后,最后一步是将请求发送到 Google Places API

$results = $google_places->getAutocomplete($params);

结果是数组。GooglePlaces 类解码了从 Google 收到的响应,因此您不需要这样做。

有关库的方法和功能的更多信息,您可以检查 "src""tests" 文件夹。

贡献

您如何贡献

  1. 进行 Fork。
  2. 创建您的功能分支(git checkout -b my-new-feature)。
  3. 提交您的更改(git commit -am '添加了一些功能')。
  4. 将更改推送到分支(git push origin my-new-feature)。
  5. 创建一个新的 Pull Request。