rodrigobendia/google-places-sdk-php

dev-master 2015-02-01 23:11 UTC

This package is auto-updated.

Last update: 2024-09-09 00:55:58 UTC


README

PHP版的Google Places SDK允许开发者在其PHP代码中使用Google Places API。这个库基于Guzzle HTTP客户端。

安装

将以下内容添加到您的composer.json文件中

{
    "require": {
      "rodrigobendia/google-places-sdk-php": "dev-master"
    }
}

使用方法

初始化

<?php

use Bendia\API\Clients\GooglePlacesApiClient;

$apiKey = 'YOUR_API_KEY';

// Initialize the instance with yout API key
GooglePlacesApiClient::instance($apiKey);

地点搜索

<?php

use Bendia\API\Clients\GooglePlacesApiClient;
use Bendia\API\Clients\GooglePlacesSearchResponse;

$location = [37.424307, -122.09502299999997];
$radius = 1000;
$sensor = false;

// Assuming that the singleton has been initialized with the API key before
$response = GooglePlacesApiClient::instance()->search($location, $radius, $sensor);

// Check response status
if ($response->isOk())
{
    for ($i = 0; $i < $response->countResults(); $i++)
    {
        $place = $response->getResult($index);
        
        echo $place->name;
        echo $place->formatted_address;
    }
}
else
{
    echo $response->getStatus();
}

文本搜索

<?php

use Bendia\API\Clients\GooglePlacesApiClient;
use Bendia\API\Clients\GooglePlacesSearchResponse;

$query = 'Google Mountain View';
$sensor = false;

// Assuming that the singleton has been initialized with the API key before
$response = GooglePlacesApiClient::instance()->searchText($query, $sensor);

// Check response status
if ($response->isOk())
{
    for ($i = 0; $i < $response->countResults(); $i++)
    {
        $place = $response->getResult($index);
        
        echo $place->name;
        echo $place->formatted_address;
    }
}
else
{
    echo $response->getStatus();
}

地点详情

<?php

use Bendia\API\Clients\GooglePlacesApiClient;
use Bendia\API\Clients\GooglePlacesDetailsResponse;

$placeId = 'ChIJYVBMERu6j4ARH8TCQcqmK6M';
$reference = null;
$sensor = false;

// Assuming that the singleton has been initialized with the API key before
$response = GooglePlacesApiClient::instance()->getDetails($placeId, $reference, $sensor);

// Check response status
if ($response->isOk())
{
    $place = $response->getResult();

    echo $place->name;
    echo $place->formatted_address;
}
else
{
    echo $response->getStatus();
}