Skip to content
Yii2 http client library based on Guzzle 6
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Yii2 HTTP client

Build Status Total Downloads


Recommended way to install this extenstion is through Composer:

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


Add this lines to your config file:

'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:

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

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

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

You can disable this behavior by specifying $detectMimeType option to whole component or single call

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

Make request with custom options:

$text = Yii::$app->httpclient->get('', [
    'proxy' => 'tcp://localhost:8125'

Read more about this options in Guzzle 6 documentation

HTTP methods

You can make request with several ways:

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

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

// Synchronous GET request
    $url, // URL
    [], // Options
    true // Detect Mime Type?

// Synchronous POST (and others) request
    $url, // URL
    $body, // Body
    [], // Options
    true // Detect Mime Type?

// Asynchronous GET request
    $url, // URL
    [] // Options

// Asynchronous POST (and others) request
    $url, // URL
    $body, // Body
    [] // Options

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

Asynchronous calls

To make an asynchronous request simly add Async to end of request method:

// PromiseInterface
$promise = Yii::$app->httpclient->postAsync('');

NOTE: mime type detection is not supported for asynchronous calls

Read more about asynchronous requests in Guzzle 6 documentation

Request body

Types you can pass as a body of request:

  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.

Read more about request body in Guzzle documentation


Feel free to send feature requests and fix bugs with Pull Requests

You can’t perform that action at this time.