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