kirianmurgadella/restapimanager

此包的最新版本(1.3.0)没有可用的许可证信息。

管理对Rest API调用的库

1.3.0 2018-08-09 07:14 UTC

This package is not auto-updated.

Last update: 2024-09-24 09:36:29 UTC


README

一个小型库,用于与任何Rest API交互。

安装

composer require kirianmurgadella/restapimanager

如何使用它

初始化ApiManager

未认证的API

使用其工厂创建ApiManager的新实例。

$apiManager = ApiManagerFactory::create('https://api-url.com');

带认证的API

此包与以下内容兼容:JSON Web TokenHTTP Basic Authentication

使用其工厂为您自己的认证创建新实例
使用JWT
$credentials = [
    'email' => 'myemail@domain.com',
    'password' => 'mypassword'
];
$apiLoginUrl = 'https://api-url.com/login';
$apiTokenRequestUrl = 'https://api-url.com/request-token';
$auth = Auth\AuthFactory::create('jwt', $credentials, $apiLoginUrl, $apiTokenRequestUrl);
使用BA
$credentials = [
    'username' => 'myusername',
    'password' => 'mypassword'
];
$auth = Auth\AuthFactory::create('basic', $credentials);
使用其工厂创建ApiManager的新实例
$apiManager = ApiManagerFactory::create('https://api-url.com', $auth);

使用ApiManager

    $apiManager->get('api/get/endpoint', ['custom headers']);
    $apiManager->post('api/post/endpoint', ['request data'], ['custom headers']);
    $apiManager->put('api/put/endpoint', ['request data'], ['custom headers']);
    $apiManager->request('custom method', 'api/custom/endpoint', ['request data'], ['custom headers']);

响应

每个响应都将具有相同的结构

[
    'status' => 'success/error',
    'statusCode => 'XXX',
    'data' => [],
    'errors' => [
        [...],
        [...],
        [...]
    ]
]
  • 状态:如果一切顺利,包含“成功”,如果失败,则包含“错误”。
  • statusCode:将是cURL请求的HTTP状态。
  • 数据:它包含请求的响应。 这里是你请求的内容
  • 错误:您希望它为空,但至少它会告诉您什么失败了。

变更日志

1.3.0

  • 添加Http Basic Authentication

1.2.0

  • 修复ApiManagerFactory命名空间。
  • 修复ApiManagerFactory使用。

1.1.0

  • 添加DELETE方法。

1.0.0

  • 基本API交互(GET、POST、PUT)。
  • 实现了JSON Web Token认证。