Skip to content

Commit 976fb30

Browse files
committed
Docs about request options added
1 parent 2f054c8 commit 976fb30

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

TransportCurl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*
1616
* Note: this transport requires PHP 'curl' extension installed.
1717
*
18+
* For this transport, you may setup request options as [cURL Options](http://php.net/manual/en/function.curl-setopt.php)
19+
*
1820
* @author Paul Klimov <klimov.paul@gmail.com>
1921
* @since 2.0
2022
*/

TransportStream.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/**
1515
* TransportStream sends HTTP messages using [Streams](http://php.net/manual/en/book.stream.php)
1616
*
17+
* For this transport, you may setup request options using [Context Options](http://php.net/manual/en/context.php)
18+
*
1719
* @author Paul Klimov <klimov.paul@gmail.com>
1820
* @since 2.0
1921
*/

docs/guide/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Usage
1515

1616
* [Data formats](usage-data-formats.md)
1717
* [Transports](usage-transports.md)
18+
* [Request options](usage-request-options.md)
1819
* [Multi-part content](usage-multi-part-content.md)
1920
* [Logging and profiling](usage-logging.md)
2021
* [Setup Client instance](usage-setup-client-instance.md)

docs/guide/usage-request-options.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Request Options
2+
===============
3+
4+
You may use [[\yii\httpclient\Request::options]] to adjust particular request execution.
5+
Following options are supported:
6+
- timeout: integer, the maximum number of seconds to allow request to be executed.
7+
- proxy: string, URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100).
8+
- userAgent: string, the contents of the "User-Agent: " header to be used in a HTTP request.
9+
- followLocation: boolean, whether to follow any "Location: " header that the server sends as part of the HTTP header.
10+
- maxRedirects: integer, the max number of redirects to follow.
11+
- sslVerifyPeer: boolean, whether verification of the peer's certificate should be performed.
12+
- sslCafile: string, location of Certificate Authority file on local filesystem which should be used with
13+
the 'sslVerifyPeer' option to authenticate the identity of the remote peer.
14+
- sslCapath: string, a directory that holds multiple CA certificates.
15+
16+
For example:
17+
18+
```php
19+
use yii\httpclient\Client;
20+
21+
$client = new Client();
22+
23+
$response = $client->createRequest()
24+
->setMethod('post')
25+
->setUrl('http://domain.com/api/1.0/users')
26+
->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
27+
->setOptions([
28+
'proxy' => 'tcp://proxy.example.com:5100', // use a Proxy
29+
'timeout' => 5, // set timeout to 5 seconds for the case server is not responding
30+
])
31+
->send();
32+
```
33+
34+
> Tip: you may setup default request options via [[\yii\httpclient\Client::requestConfig]]. If you do so,
35+
use [[\yii\httpclient\Request::addOptions()]] to preserve their values, if you wish to add extra specific
36+
options for request.
37+
38+
You may as well pass options, which are specific for particular request transport. Usually it comes to this
39+
in case of using [[\yii\httpclient\TransportCurl]]. For example: you may want to specify separated timeout
40+
for connection and receiving data, which supported by PHP cURL library. You can do this in following way:
41+
42+
```php
43+
use yii\httpclient\Client;
44+
45+
$client = new Client([
46+
'transport' => 'yii\httpclient\TransportCurl' // only cURL supports the options we need
47+
]);
48+
49+
$response = $client->createRequest()
50+
->setMethod('post')
51+
->setUrl('http://domain.com/api/1.0/users')
52+
->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
53+
->setOptions([
54+
CURLOPT_CONNECTTIMEOUT => 5, // connection timeout
55+
CURLOPT_TIMEOUT => 10, // data receiving timeout
56+
])
57+
->send();
58+
```
59+
60+
Please refer to the particular transport class docs for details about specific options support.

0 commit comments

Comments
 (0)