albawebstudio / petfinder-api
Laravel 包,用于集成 Petfinder API
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.2
- illuminate/support: ^9|^10
README
以下库可以帮助您使用 Petfinder API。
安装
使用 composer 安装此包。建议仅将此包用于开发。
composer require albawebstudio/petfinder-api
Laravel 使用包自动发现,因此不需要您手动添加 ServiceProvider。
Laravel 不使用自动发现
如果您不使用自动发现,请将 ServiceProvider 添加到 config/app.php 中的 providers 数组中。
albawebstudio\PetfinderApi\ServiceProvider::class,
您也可以通过 .env 变量设置 Petfinder API 密钥、API 密码和组织 ID。
使用发布命令将包配置复制到您的本地配置
php artisan vendor:publish --provider="albawebstudio\\PetfinderApi\\ServiceProvider"
在 Laravel 项目中使用
您可以使用以下说明来实现请求。
更新 .env 文件
将以下内容追加到您的 .env
文件末尾。您可以在 Petfinder 账户的 开发者设置 中找到 API 访问信息。
您可以通过几种方式获取您的组织 ID。查看 动物收容所和救助机构 页面并搜索您的组织。在“收容所搜索结果”中,将鼠标悬停在“宠物列表”列中的搜索图标上。您的组织 ID 是 shelter_id=
后的值。
例如,如果您悬停在以下 URL 上 https://www.petfinder.com/pet-search?shelter_id=MN452
,则您的组织 ID 是 MN452
。
# PETFINDER
PETFINDER_API_KEY=
PETFINDER_API_SECRET=
PETFINDER_ORGANIZATION_ID=
为发送 API 请求创建一个新的控制器。我们的示例中创建了一个控制器 PetfinderApiController
。
<?php namespace App\Http\Controllers; use albawebstudio\PetfinderApi\PetfinderConnector; class PetfinderApiController extends Controller { public function __construct() { PetfinderConnector::init( config('petfinder.key'), config('petfinder.secret'), config('petfinder.organization'), ); } }
下一步是生成一个新的控制器来获取 animals
数据。我们创建了一个控制器 AnimalController
。
*注意:控制器将扩展 PetfinderApiController
。
<?php namespace App\Http\Controllers; use albawebstudio\PetfinderApi\Animal; use albawebstudio\PetfinderApi\exceptions\InvalidAuthorizationException; use albawebstudio\PetfinderApi\exceptions\InvalidRequestException; use albawebstudio\PetfinderApi\exceptions\PetfinderConnectorException; class AnimalController extends PetfinderApiController { /** * @var Animal */ protected Animal $api; public function __construct() { parent::__construct(); $this->api = new Animal(); } /** * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function index() { return $this->api->animals([]); } /** * @param int $animalId * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function show(int $animalId): array { return $this->api->animal($animalId); } /** * Return all animal types from Petfinder * * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function types(): array { return $this->api->types(); } /** * Return specific animal type from Petfinder * * @param string $type * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function type(string $type): array { return $this->api->type($type); } /** * Return all breeds for specific animal type from Petfinder * @param string $type * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function breeds(string $type): array { return $this->api->breeds($type); } }
AnimalController
具有通过 Petfinder API 获取所有现有动物信息的必要方法。请务必查看所有可用的 查询参数。
您可以选择为获取 organizations
数据生成一个新的控制器。我们创建了一个控制器 OrganizationController
。
*注意:控制器将扩展 PetfinderApiController
。
<?php namespace App\Http\Controllers; use albawebstudio\PetfinderApi\exceptions\InvalidAuthorizationException; use albawebstudio\PetfinderApi\exceptions\InvalidRequestException; use albawebstudio\PetfinderApi\exceptions\PetfinderConnectorException; use albawebstudio\PetfinderApi\Organization; class OrganizationController extends PetfinderApiController { /** * @var Organization */ protected Organization $api; public function __construct() { parent::__construct(); $this->api = new Organization(); } /** * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function index(): array { return $this->api->organizations([]); } /** * @param string $organizationId * @return array * @throws InvalidAuthorizationException * @throws InvalidRequestException * @throws PetfinderConnectorException */ public function show(string $organizationId): array { return $this->api->organization($organizationId); } }
OrganizationController
具有通过 Petfinder API 获取所有现有组织的必要方法。请务必查看所有可用的 查询参数。
API 路由
以下是如何设置 API 路由的示例。将以下内容添加到 routes/api.php
Route::prefix('animals')->group(function() { Route::get('/', 'App\Http\Controllers\AnimalController@index')->name('animals'); Route::get('/{id}', 'App\Http\Controllers\AnimalController@show')->name('animal'); }); Route::prefix('types')->group(function () { Route::get('/', 'App\Http\Controllers\AnimalController@types')->name('types'); Route::get('/{type}', 'App\Http\Controllers\AnimalController@type')->name('type'); Route::get('/{type}/breeds', 'App\Http\Controllers\AnimalController@breeds')->name('breeds'); }); Route::prefix('organizations')->group(function() { Route::get('/', 'App\Http\Controllers\OrganizationController@index')->name('organizations'); Route::get('/{id}', 'App\Http\Controllers\OrganizationController@show')->name('organization'); });