herui/apiclient

This package is abandoned and no longer maintained. The author suggests using the https://gitlab-tech.shruisong.cn/yingxiu/api-client package instead.

PHP API client library for MSA API

dev-master 2021-03-18 06:22 UTC

This package is not auto-updated.

Last update: 2021-03-23 03:01:49 UTC


README

安装

  1. 修改composer.json

    {
     "require":
     {
         "herui/apiclient": "dev-master"
     },
     "repositories":
     [
         {
             "type": "git",
             "url":  "git@gitlab-tech.shruisong.cn:yingxiu/api-client.git"
         }
     ]
    }
    
  2. Laravel修改config/app.php

    <?php
    return [
     'providers' => [
         /*
          * Package Service Providers...
          */
         Herui\API\Provider\LaravelServiceProvider::class,
     ],
     'aliases' => [
         'APIClient' => Herui\API\Facade\APIClient::class,
     ]
    ];
    
  3. Lumen修改bootstrap/app.php

    <?php
    $app->register(Herui\API\Provider\LumenServiceProvider::class);
    if (!class_exists('APIClient')) {
     class_alias(Herui\API\Facade\APIClient::class, 'APIClient');
    }
    
  4. 修改.env.example和.env文件

    API_BASE_URI=https://127.0.0.1/api/
    API_VERIFY=false
    API_CLIENT_ID=
    API_CLIENT_SECRET=
    API_REDIRECT_URI=https://127.0.0.1/
    API_URL_AUTHORIZE=https://127.0.0.1/oauth/authorize
    API_URL_ACCESS_TOKEN=https://127.0.0.1/oauth/token
    API_RESOURCE_OWNER_DETAILS=https://127.0.0.1/api/me/profile
    

使用

<?php
class Test
{
  protected $client;
  
  public function __construct(\Herui\API\Client $client)
  {
    self::initAccessToken();
    $this->client = $client;
  }
  
  public static function initAccessToken()
  {
    // using client credentials
    $accessToken = APIClient::getAccessToken('client_credentials');
    
    // using password
    $accessToken = APIClient::getAccessToken('password', [
      'username' => 'herui:12345646546',
      'password' => '888888',
    ]);
    
    // using refresh token
    $accessToken = APIClient::getAccessToken('refresh_token', [
      'refresh_token' => 'de873f1fbf02fc97a289ad377a491ff6',
    ]);
    
    // save access token globally
    APIClient::setAccessToken($accessToken);
  }
  
  public function callAPI()
  {
    // this client extends from GuzzleHttp/Client
    // visit documents at http://docs.guzzlephp.org/en/latest/ 
    $response = $this->client->get('me/profile');
    $json = $response->getBody()->getContent();
  }
}