sspat/profiru

此包已被废弃,不再维护。没有建议的替代包。

Profi.ru 合作伙伴计划 API 客户端

2.0.0 2018-12-17 13:20 UTC

This package is auto-updated.

Last update: 2020-12-30 16:14:43 UTC


README

Author GitHub tag license

特性

此包提供了访问 Profi.ru 合作伙伴计划 API 的便捷方式。

在注册计划并获得 SSL 证书和密钥后,您可以立即使用 API。

当前实现的包中包含 dictionarypagination API。在未来的版本中,将实现 provisioning API。

要求

  • 最低要求的 PHP 版本是 PHP 7.3.0。
  • 您需要从 Profi.ru 合作伙伴计划代表那里获得的 SSL 证书和公钥才能访问 API。

安装

安装此扩展的首选方式是通过 composer

运行以下命令

composer require sspat/profiru

或者将以下内容添加到您的 composer.json 文件的 require 部分。

"sspat/profiru": "*"

使用方法

向 API 发送请求

use GuzzleHttp\RequestOptions;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use sspat\ProfiRu\Client;
use sspat\ProfiRu\Constants\Domains;

// Create an instance of any PSR-18 compliant HTTP Client and pass your partnership SSL certificate and key paths
$httpClient = GuzzleAdapter::createWithConfig([
    RequestOptions::TIMEOUT => 5,
    RequestOptions::SSL_KEY => 'partner.key',
    RequestOptions::CERT => 'partner.crt',
    RequestOptions::VERIFY => false,
]);
// Create an instance of the API client, passing the HTTP client to it
$api = new Client($httpClient);

// Get Locations dictionary
$locations = $api->getLocations();
// Get Services dictionary
$services = $api->getServices();

// For requests to the pagination API an API domain must be specified
$organizations = $api->getOrganizations(Domains::HEALTHCARE);
$specialists = $api->getSpecialists(Domains::BEAUTY);

pagination API 的请求需要选择一个 API 域名,并可以通过第二个参数进一步配置。

$organizations = $api->getOrganizations(
    Domains::HEALTHCARE,
    [
         'city'   => Cities::MOSCOW,
         'from'   => 220,
         'count'  => 10,
         'scope'  => Scopes::SCOPE_FULL,
         'ip'     => '144.135.23.1',
         'models' => [
             Models::ASSOCIATION,
             Models::ASSOCIATION_STRUCTURE_UNIT
         ]
    ]
);

pagination API 支持的域名

域名 描述 支持的城市
dktr 医疗保健 msk, spb
krst 美容 msk, spb

pagination API 支持的附加参数

参数 描述 默认值 可能值 常量类
city 分页 API 一次只能返回单个城市的条目。不同的域名支持不同的城市集合。所有域名都支持莫斯科,所以它是默认值。 msk 依赖于域名 \sspat\ProfiRu\Constants\Cities
from 用于跳过分页中的条目数量 0 正整数
count 每页条目数 20 1-20
scope 定义每个条目检索的数据量 profile.mini profile.mini, profile.full \sspat\ProfiRu\Constants\Scopes
ip API 客户端的 IP 地址 127.0.0.1 IPv4/IPv6 地址
models API 中要获取的条目类型 取决于您是否请求组织或专家 取决于您是否请求组织或专家 \sspat\ProfiRu\Constants\Models

获取响应

客户端返回已包含 API 响应的数组形式的响应对象。如果您需要额外的响应数据,还可以检索 PSR-7 响应对象。

// Get Locations dictionary
$locations = $api->getLocations();
// Get response body as Array
$locationsArray = $locations->asArray();
// Get PSR-7 response object
$psrResponse = $locations->response();
var_dump((string) $psrResponse->getBody());

处理错误

响应对象处理 API 返回的所有错误,并以异常的形式抛出。

use sspat\ProfiRu\Exceptions\ErrorResponse;

try {
    $services = $api->getServices();
} catch (ErrorResponse $e) {
    var_dump($e->getErrors());
}