stevenmaguire/yelp-php

用于消费 Yelp API 的 PHP 客户端

2.2.0 2018-12-12 21:38 UTC

This package is auto-updated.

Last update: 2024-09-14 11:13:06 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

使用 OAuth 认证并与 Yelp API 交互的 PHP 客户端。

安装

通过 Composer

$ composer require stevenmaguire/yelp-php

用法

此包目前支持 Yelp API 的 v2v3(融合)版本。每个版本的 Yelp API 都对应不同的客户端,因为 API 非常不同。每个客户端都有单独的文档;以下提供链接。

创建客户端

由于 Yelp API 的不同,每个版本对应不同的客户端。有一个客户端工厂可以创建适当的客户端。

v2 客户端示例

$options = array(
    'consumerKey' => 'YOUR COSUMER KEY',
    'consumerSecret' => 'YOUR CONSUMER SECRET',
    'token' => 'YOUR TOKEN',
    'tokenSecret' => 'YOUR TOKEN SECRET',
    'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
);

$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
    $options,
    \Stevenmaguire\Yelp\Version::TWO
);

v3 客户端示例

$options = array(
    'accessToken' => 'YOUR ACCESS TOKEN', // Required, unless apiKey is provided
    'apiHost' => 'api.yelp.com', // Optional, default 'api.yelp.com',
    'apiKey' => 'YOUR ACCESS TOKEN', // Required, unless accessToken is provided
);

$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
    $options,
    \Stevenmaguire\Yelp\Version::THREE
);

在 2017 年 12 月 7 日之前,需要 accessToken 来进行身份验证。从那时起,apiKey 是首选的身份验证方法。此库支持 accessTokenapiKey,如果都提供了,则优先使用 apiKey

https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going

从最近的请求中获取速率限制数据

对于 v3 客户端,在最近的请求后,速率限制数据 可用。

// $latestRateLimit will be null if an http request hasn't been successfully completed.
$latestRateLimit = $client->getRateLimit();

// The maximum number of calls you can make per day
$latestDailyLimit = $latestRateLimit->dailyLimit;

// The number of calls remaining within the current day
$latestRemaining = $latestRateLimit->remaining;

// The time at which the current rate limiting window will expire as an ISO 8601 timestamp
$latestResetTime = $latestRateLimit->resetTime;

// The time at which the current rate limiting data was observed as an ISO 8601 timestamp
$latestCreatedAt = $latestRateLimit->createdAt;

异常

如果 API 请求导致 Http 错误,客户端将抛出包含来自 Yelp API 的响应体(作为字符串)的 Stevenmaguire\Yelp\Exception\HttpException

$responseBody = $e->getResponseBody(); // string from Http request
$responseBodyObject = json_decode($responseBody);

高级用法

v3 客户端和 v2 客户端都公开了一些方法,允许通过提供替代的 HTTP 客户端和请求来覆盖默认行为。

$client = new \Stevenmaguire\Yelp\v3\Client(array(
    'accessToken' => $accessToken,
));

// Create a new guzzle http client
$specialHttpClient = new \GuzzleHttp\Client([
    // ... some special configuration
]);

// Update the yelp client with the new guzzle http client
// then get business data
$business = $client->setHttpClient($specialHttpClient)
    ->getBusiness('the-motel-bar-chicago');

// Create request for other yelp API resource not supported by yelp-php
$request = $client->getRequest('GET', '/v3/some-future-endpoint');

// Send that request
$response = $client->getResponse($request);

// See the contents
echo $response->getBody();

yelp-php 1.x 升级到 yelp-php 2.x 支持 Yelp API v2

// same options for all
$options = array(
    'consumerKey' => 'YOUR COSUMER KEY',
    'consumerSecret' => 'YOUR CONSUMER SECRET',
    'token' => 'YOUR TOKEN',
    'tokenSecret' => 'YOUR TOKEN SECRET',
    'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
);


// yelp-php 1.x
$client = new Stevenmaguire\Yelp\Client($options);

// yelp-php 2.x - option 1
$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
    $options,
    \Stevenmaguire\Yelp\Version::TWO
);

// yelp-php 2.x - option 2
$client = new \Stevenmaguire\Yelp\v2\Client($options);

测试

$ ./vendor/bin/phpunit

贡献

请参阅 CONTRIBUTING 了解详细信息。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件