2017 © Pedro Peláez
 

extension yii2-httpclient

Yii2 http client implementation based on Guzzle library

image

understeam/yii2-httpclient

Yii2 http client implementation based on Guzzle library

  • Saturday, August 27, 2016
  • by AnatolyRugalev
  • Repository
  • 3 Watchers
  • 14 Stars
  • 23,919 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 6 Versions
  • 5 % Grown

The README.md

Yii2 HTTP client

Build Status Total Downloads, (*1)

Installation

Recommended way to install this extenstion is through Composer:, (*2)

php composer.phar require understeam/yii2-httpclient:~1.0 --prefer-dist

Configuration

Add this lines to your config file:, (*3)

...
'components' => [
    'httpclient' => [
        'class' =>'understeam\httpclient\Client',
        'detectMimeType' => true, // automatically transform request to data according to response Content-Type header
        'requestOptions' => [
            // see guzzle request options documentation
        ],
        'requestHeaders' => [
            // specify global request headers (can be overrided with $options on making request)
        ],
    ],
],
...

Basic usage

Performing HTTP GET request with mime type detection:, (*4)

// Result is html text
$text = Yii::$app->httpclient->get('http://httpbin.org/html');

// Result is SimpleXMLElement containing parsed XML
$xml = Yii::$app->httpclient->get('http://httpbin.org/xml');

// Result is parsed JSON array
$json = Yii::$app->httpclient->get('http://httpbin.org/get');

You can disable this behavior by specifying $detectMimeType option to whole component or single call, (*5)

// Result is Guzzle `Response` object
$text = Yii::$app->httpclient->get('http://httpbin.org/xml', [], false);

Make request with custom options:, (*6)

$text = Yii::$app->httpclient->get('http://httpbin.org/xml', [
    'proxy' => 'tcp://localhost:8125'
]);

Read more about this options in Guzzle 6 documentation, (*7)

HTTP methods

You can make request with several ways:, (*8)

  1. Call shortcut method (get(), post(), put(), delete(), etc.)
  2. Call request() method

All shortcut methods has the same signature except get():, (*9)

// Synchronous GET request
Yii::$app->httpclient->get(
    $url, // URL
    [], // Options
    true // Detect Mime Type?
);

// Synchronous POST (and others) request
Yii::$app->httpclient->post(
    $url, // URL
    $body, // Body
    [], // Options
    true // Detect Mime Type?
);

// Asynchronous GET request
Yii::$app->httpclient->getAsync(
    $url, // URL
    [] // Options
);

// Asynchronous POST (and others) request
Yii::$app->httpclient->postAsync(
    $url, // URL
    $body, // Body
    [] // Options
);

NOTE: you still can make a GET request with body via request() function, (*10)

Asynchronous calls

To make an asynchronous request simly add Async to end of request method:, (*11)

// PromiseInterface
$promise = Yii::$app->httpclient->postAsync('http://httpbin.org/post');

NOTE: mime type detection is not supported for asynchronous calls, (*12)

Read more about asynchronous requests in Guzzle 6 documentation, (*13)

Request body

Types you can pass as a body of request:, (*14)

  1. __Arrayable object__ (ActiveRecord, Model etc.) - will be encoded into JSON object
  2. Array - will be sent as form request (x-form-urlencoded)

Any other data passed as body will be sent into Guzzle without any transformations., (*15)

Read more about request body in Guzzle documentation, (*16)

Appendix

Feel free to send feature requests and fix bugs with Pull Requests, (*17)

The Versions

27/08 2016

dev-master

9999999-dev

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires

20/03 2016

v1.0

1.0.0.0

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires

27/01 2016

0.3

0.3.0.0

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires

23/09 2015

dev-guzzle-6.1

dev-guzzle-6.1

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires

29/06 2015

0.2

0.2.0.0

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires

01/04 2015

0.1

0.1.0.0

Yii2 http client implementation based on Guzzle library

  Sources   Download

GPL-v3

The Requires

 

The Development Requires