rymanalu/http

由Guzzle驱动的简单HTTP客户端库。

v0.2.1 2017-03-30 11:29 UTC

This package is auto-updated.

Last update: 2024-09-09 15:26:21 UTC


README

Build Status

此包提供了一种新的使用GuzzleHttp包的方式。

安装

通过Composer包管理器安装此包

composer require rymanalu/http

用法

案例:我们想要调用http://foo.com/bar API。

步骤

  • 通过扩展Rymanalu\Http\Endpoint创建一个新的端点。例如
use Rymanalu\Http\Endpoint;

class BarEndpoint extends Endpoint
{
    /**
     * The endpoint's URI.
     *
     * @var string
     */
    protected $uri = 'bar';

    /**
     * The endpoint's method.
     *
     * @var string
     */
    protected $method = 'POST';
}
  • 创建一个新的Rymanalu\Http\Client实例
$http = new Rymanalu\Http\Client('http://foo.com');
  • 通过传递BarEndpoint实例,在Rymanalu\Http\Client实例中调用call方法
$endpoint = new BarEndpoint; // or `new BarEndpoint($anArrayOfGuzzleOptions)`

$response = $http->call($endpoint);
  • $response将是一个Rymanalu\Http\Response对象
// Check if the calls is successful by the response code...
$response->isSuccessful(); // `true` or `false`

// Get the response body...
$response->getBody(); // object of `stdClass`
$response->getBody(true); // array