rgen3/restapi-core-client

JSON-RCP 简单客户端。

安装次数: 6

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:php-library

dev-master 2018-03-31 01:13 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:27:53 UTC


README

此仓库提供简单的 curl 使用方法

安装

composer require rgen3/restapi-core-client

主要功能

  • 默认使用 json 请求
  • 为每个请求生成 guid 并作为 X-Request-Id 标头传递
  • 支持异步 curl 请求
  • 易于添加 curl 选项
  • 简单提供数据
  • 一个类对应一个方法
  • 支持通过 GET 参数传递 access-token

使用示例

代码示例使用在 example 文件夹中

<?php

use rgen3\json\client\core\AbstractMethod;

class TestRequest extends AbstractMethod
{
    public function getBaseUrl()
    {
        return 'https://httpstat.us';
    }

    public function getUrlPath()
    {
        return '/200';
    }

    public function getType()
    {
        return 'GET';
    }

    public function getData()
    {
        // Sends GET data to sleep 50 seconds
        return [
            'sleep' => '2000'
        ];
    }

    public function waitAnswer()
    {
        // if you do not want to wait for an answer
        // set here false
        return true;
    }
}

在创建文件后,在您的代码中调用它

// data you want to send
$data = [
    'any' => 'array'
];
$request = (new TestRequest($data))->executer();

$request->getResult();
// and same method
$request->result;

// Get curl response headers
$request->getCurl()->getResponseHeaders();

// Get response status
$request->getResponseStatus();

如果您想将令牌作为 GET 参数提供,则必须在使用需要令牌的方法之前使用

rgen3\json\client\core\Fabric::setToken('needed-token');

注意:在异步模式(当 waitAnswer 返回 false)下使用时,您不能使用与 curl 响应相关的任何方法

方法

您可以在您的 TestRequest 类中使用以下任何方法来修改行为

/**
 * Makes request asynchronous
 *
 * You woun't waste time waiting for a request answer
 * But you will not get any response
 * for an answer,
 *
 * @return bool
 */
public function waitAnswer()
{
    // if you do not want to wait for an answer
    // set return value to `false`
    return true;
}

/**
 * Sets the type of the request
 * i.e. 'GET', 'POST', 'PUT', 'DELETE', etc.
 *
 * @return mixed
 */
public function getType()
{
    // Default value to be returned
    return 'POST';
}

public function getBaseUrl()
{
    // base url you want to use for this method
    return 'https://httpstat.us';
}

/**
 * Sets the url of a method to be call
 *
 * @return mixed
 */
public function getUrlPath()
{
    // path without url
    // slashes will be trimmed
    return '/pathname';
}

/**
 * Returns the data to be send
 *
 * @return mixed
 */
public function getData()
{
    // return any data you want
    // as default you have to json compatible data
    // because of json_encode method in `AbstractMethod` class
    return [];
}

/**
 * Processes
 *
 * @param $data
 * @return IMethod
 */
public function processResult($data);

/**
 * Returns request results
 * @return mixed
 */
public function getResult()
{
    // returns result from curl
    return [];
}

/**
 * @return array
 */
public function getCurlOptions()
{
    // Here you can provide additional curl parameters
    return [];
}

/**
 * @return bool
 */
public function tokenRequired() 
{
    // return true if you want to send token as get parameter
    return true;
}

/**
 * Returns token key
 * @return string
 */
public function getTokenKey()
{
    // You can set any value you want to see in GET query parameter for token
    // default is
    return 'access-token';
}


/**
 * Prepares data to be sent
 * For all method except GET
 * @return array
 */
public function getRequestData()
{
   $array = [];
   // any code you want
   return $array;
}