Skip to content

Commit da9f4a8

Browse files
committed
Docs updated
1 parent 10188a7 commit da9f4a8

8 files changed

+287
-10
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,3 @@ or add
2828
```
2929

3030
to the require section of your composer.json.
31-
32-
33-
Configuration
34-
-------------
35-
36-
To use this extension, simply

docs/guide/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ Getting Started
88
---------------
99

1010
* [Installation](installation.md)
11-
* [Quick Start](quick-start.md)
11+
* [Basic Usage](basic-usage.md)
12+
13+
Usage
14+
-----
15+
16+
* [Data formats](usage-data-formats.md)
17+
* [Transports](usage-transports.md)
18+
* [Logging and profiling](usage-logging.md)
19+
* [Setup Client instance](usage-setup-client-instance.md)
20+
21+
Additional topics
22+
-----------------
23+
24+
* [Using the HTTP client DebugPanel](topics-debug.md)

docs/guide/quick-start.md renamed to docs/guide/basic-usage.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
Quick Start
1+
Basic Usage
22
===========
33

4-
## Basic usage
5-
64
In order to send HTTP requests, you'll need to instantiate [[\yii\httpclient\Client]] and use its
75
`createRequest()` method to create new HTTP request. Then you should configure all request parameters
86
according to your goal and send request. As the result you'll get a [[\yii\httpclient\Response]] instance,

docs/guide/topics-debug.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Using the HTTP Client DebugPanel
2+
================================
3+
4+
The yii2 HTTP Client extension provides a debug panel that can be integrated with the yii debug module
5+
and shows the executed HTTP requests.
6+
7+
Add the following to you application config to enable it (if you already have the debug module
8+
enabled, it is sufficient to just add the panels configuration):
9+
10+
```php
11+
// ...
12+
'bootstrap' => ['debug'],
13+
'modules' => [
14+
'debug' => [
15+
'class' => 'yii\\debug\\Module',
16+
'panels' => [
17+
'httpclient' => [
18+
'class' => 'yii\\httpclient\\debug\\HttpClientPanel',
19+
],
20+
],
21+
],
22+
],
23+
// ...
24+
```
25+
26+
This panel allows you execution of the logged HTTP request to see its response. You may get the response as
27+
a text representation or pass it directly to the browser.
28+
29+
> Note: only regular logged HTTP requests can be executed via debug panel, requests sending in batch can not.
30+
Also keep in mind that content of logged request can be trimmed according to [[\yii\httpclient\Client::contentLoggingMaxSize]],
31+
so its execution may fail or produce unexpected results.

docs/guide/usage-data-formats.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Data Formats
2+
============
3+
4+
Data format determines the way content of HTTP message should be composed or parsed, e.g. it determines
5+
how [[\yii\httpclient\Message::data]] should be converted into [[\yii\httpclient\Message::content]] and vice versa.
6+
7+
Following formats are supported by default:
8+
9+
- [[\yii\httpclient\Client::FORMAT_JSON]] - JSON format
10+
- [[\yii\httpclient\Client::FORMAT_URLENCODED]] - urlencoded by RFC1738 query string
11+
- [[\yii\httpclient\Client::FORMAT_RAW_URLENCODED]] - urlencoded by PHP_QUERY_RFC3986 query string
12+
- [[\yii\httpclient\Client::FORMAT_XML]] - XML format
13+
14+
Each format is covered by 2 entities: 'formatter' and 'parser'. Formatter determines the way content of the
15+
request should be composed from data. Parser determines how raw response content should be parsed into data.
16+
17+
[[\yii\httpclient\Client]] automatically chooses corresponding formatter and parser for all format mentioned above.
18+
However you can alter this behavior using [[\yii\httpclient\Client::formatters]] and [[\yii\httpclient\Client::parsers]].
19+
With these fields you can add you own formats or alter standard ones.
20+
For example:
21+
22+
```php
23+
use yii\httpclient\Client;
24+
25+
$client = new Client([
26+
'formatters' => [
27+
'myformat' => 'app\components\http\MyFormatter', // add new formatter
28+
Client::FORMAT_XML => 'app\components\http\MyXMLFormatter', // override default XML formatter
29+
],
30+
]);
31+
```
32+
33+
While creating your own parser you should implement [[\yii\httpclient\ParserInterface]], while creating
34+
formatter - [[\yii\httpclient\FormatterInterface]]. For example:
35+
36+
```php
37+
use yii\httpclient\FormatterInterface;
38+
use yii\httpclient\ParserInterface;
39+
use yii\httpclient\Response;
40+
41+
class ParserIni implements ParserInterface
42+
{
43+
public function parse(Response $response)
44+
{
45+
return parse_ini_string($response->content);
46+
}
47+
}
48+
49+
class FormatterIni implements FormatterInterface
50+
{
51+
public function format(Request $request)
52+
{
53+
$request->getHeaders()->set('Content-Type', 'text/ini ; charset=UTF-8');
54+
55+
$pairs = []
56+
foreach ($request->data as $name => $value) {
57+
$pairs[] = "$name=$value";
58+
}
59+
60+
$request->setContent(implode("\n", $pairs));
61+
return $request;
62+
}
63+
}
64+
```

docs/guide/usage-logging.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Logging and Profiling
2+
=====================
3+
4+
This extension allows logging HTTP requests being sent and profiling their execution.
5+
In order to setup a log target, which can capture all entries related to HTTP requests, you should
6+
use category `yii\httpclient\Transport*`. For example:
7+
8+
```php
9+
return [
10+
// ...
11+
'components' => [
12+
// ...
13+
'log' => [
14+
// ...
15+
'targets' => [
16+
[
17+
'class' => 'yii\log\FileTarget',
18+
'logFile' => '@runtime/logs/http-request.log',
19+
'categories' => ['yii\httpclient\Transport*'],
20+
],
21+
// ...
22+
],
23+
],
24+
],
25+
];
26+
```
27+
28+
You may as well use [HTTP client DebugPanel](topics-debug.md) to see all related logs.
29+
30+
> Attention: since content of the some HTTP requests may be very long, saving it in full inside the logs
31+
may lead to certain problems. Thus there is a restriction on the maximum length of the request content,
32+
which will be placed in log. It is controlled by [[\yii\httpclient\Client::contentLoggingMaxSize]].
33+
Any exceeding content will be trimmed before logging.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Setup Client instance
2+
=====================
3+
4+
Using of this extension starts from instantiating [[\yii\httpclient\Client]] object. There are several ways
5+
you can integrate [[\yii\httpclient\Client]] to your program. Here the most common approaches are described.
6+
7+
8+
## Setup client as application component
9+
10+
[[\yii\httpclient\Client]] extends [[\yii\base\Component]] and thus it can be setup at [[\yii\di\Container]]
11+
level: as module or application component. For example:
12+
13+
```php
14+
return [
15+
// ...
16+
'components' => [
17+
// ...
18+
'phpNetHttp' => [
19+
'class' => 'yii\httpclient\Client',
20+
'baseUrl' => 'http://uk.php.net',
21+
],
22+
],
23+
];
24+
25+
// ...
26+
echo Yii::$app->phpNetHttp->get('docs.php')->send()->content;
27+
```
28+
29+
30+
## Extending client class
31+
32+
Since [[\yii\httpclient\Client]] can be used as application component, you can just extend it, adding some
33+
custom logic, which you need. For example:
34+
35+
```php
36+
use yii\httpclient\Client;
37+
38+
class MyRestApi extends Client
39+
{
40+
public $baseUrl = 'http://my.rest.api/';
41+
42+
public function addUser(array $data)
43+
{
44+
$response = $this->post('users', $data)->send();
45+
if (!$response->isOk) {
46+
throw new \Exception('Unable to add user.');
47+
}
48+
return $response->data['id'];
49+
}
50+
51+
// ...
52+
}
53+
```
54+
55+
56+
## Wrapping client object
57+
58+
You may use [[\yii\httpclient\Client]] instance as internal field for component, which provides some complex
59+
functionality. For example:
60+
61+
```php
62+
use yii\base\Component;
63+
use yii\httpclient\Client;
64+
65+
class MyRestApi extends Component
66+
{
67+
public $baseUrl = 'http://my.rest.api/';
68+
69+
private $_httpClient;
70+
71+
public function getHttpClient()
72+
{
73+
if (!is_object($this->_httpClient)) {
74+
$this->_httpClient = Yii::createObject([
75+
'class' => Client::className(),
76+
'baseUrl' => $this->baseUrl,
77+
]);
78+
}
79+
return $this->_httpClient;
80+
}
81+
82+
public function addUser(array $data)
83+
{
84+
$response = $this->getHttpClient()->post('users', $data)->send();
85+
if (!$response->isOk) {
86+
throw new \Exception('Unable to add user.');
87+
}
88+
return $response->data['id'];
89+
}
90+
91+
// ...
92+
}
93+
```

docs/guide/usage-transports.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Transports
2+
==========
3+
4+
[[\yii\httpclient\Client]] provides several different ways to actually send an HTTP message - several transports.
5+
Predefined transports are:
6+
7+
- [[\yii\httpclient\TransportStream]] - sends HTTP messages using [Streams](http://php.net/manual/en/book.stream.php).
8+
This transport is used by default. It does not require any additional PHP extensions or libraries installed,
9+
but does not support advanced features like batch sending.
10+
- [[\yii\httpclient\TransportCurl]] - sends HTTP messages using [Client URL Library (cURL)](http://php.net/manual/en/book.curl.php)
11+
This transport requires PHP 'curl' extension to be installed, but provides support for advanced features, like
12+
batch sending.
13+
14+
You may configure the transport to be used by particular client using [[\yii\httpclient\Client::transport]]:
15+
16+
```php
17+
use yii\httpclient\Client;
18+
19+
$client = new Client([
20+
'transport' => 'yii\httpclient\TransportCurl'
21+
]);
22+
```
23+
24+
25+
## Creating custom transport
26+
27+
You may create your own transport, which will perform message sending in its own way. To do so you should
28+
extend [[\yii\httpclient\Transport]] class and implement at least `send()` method. All you need to do is
29+
determine HTTP response content and headers, then you can compose a response object from them using
30+
[[\yii\httpclient\Client::createResponse()]]:
31+
32+
```php
33+
use yii\httpclient\Transport;
34+
35+
class MyTransport extends Transport
36+
{
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function send($request)
41+
{
42+
$responseContent = '???';
43+
$responseHeaders = ['???'];
44+
45+
return $request->client->createResponse($responseContent, $responseHeaders);
46+
}
47+
}
48+
```
49+
50+
You may as well override `batchSend()` method if there is a way to send multiple requests with better performance
51+
like sending them asynchronously in parallel.

0 commit comments

Comments
 (0)