pecyn/api-wrapper

1.0.0 2020-11-22 21:43 UTC

This package is auto-updated.

Last update: 2024-09-23 08:06:30 UTC


README

这是一个简单的包,帮助生成API包装器。

安装

composer require pecyn/api-wrapper

使用

创建一个表示您的API的类。

<?php

namespace App\EndPoints;

use Pecyn\ApiWrapper\EndPoint;

class CatsGetEndPoint extends EndPoint
{
    protected $method = 'GET';
    protected $uri = 'https://cat-fact.herokuapp.com/facts';

    public function __construct(string $id)
    {
        $this->uri .= '/' . $id;
    }

    public function processResponse(\Psr\Http\Message\ResponseInterface $response)
    {
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
    }
}

创建一个API实例

<?php

use Pecyn\ApiWrapper\Api;

$api = new \Pecyn\ApiWrapper\Api(['end_point_namespaces' => ['App\EndPoints']]);
//or
$api = new \Pecyn\ApiWrapper\Api();
$api->setEndPointNameSpaces(['App\EndPoints']);
//or extend the Api class.
//class MyApi Extends Api{
//
//protected $endPointNameSpaces = ['App\EndPoints'];
//      

$facts = $api->cats->get('591d9b2f227c1a0020d26823');