|
| 1 | +Quick Start |
| 2 | +=========== |
| 3 | + |
| 4 | +## Basic usage |
| 5 | + |
| 6 | +In order to send HTTP requests, you'll need to instantiate [[\yii\httpclient\Client]] and use its |
| 7 | +`createRequest()` method to create new HTTP request. Then you should configure all request parameters |
| 8 | +according to your goal and send request. As the result you'll get a [[\yii\httpclient\Response]] instance, |
| 9 | +which holds all response information and data. |
| 10 | +For example: |
| 11 | + |
| 12 | +```php |
| 13 | +use yii\httpclient\Client; |
| 14 | + |
| 15 | +$client = new Client(); |
| 16 | +$response = $client->createRequest() |
| 17 | + ->setMethod('post') |
| 18 | + ->setUrl('http://domain.com/api/1.0/users') |
| 19 | + ->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com']) |
| 20 | + ->send(); |
| 21 | +if ($response->isOk) { |
| 22 | + $newUserId = $response->data['id']; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +You may use shortcut methods `get()`, `post()`, `put()` and so on to simplify new request preparation. |
| 27 | +If you are using single [[\yii\httpclient\Client]] instance for multiple request to the same domain (for |
| 28 | +example in case of using REST API), you may setup its `baseUrl` property with this domain. This will |
| 29 | +allow you to specify only the relative URL while creating a new request. |
| 30 | +Thus the several request to some REST API may look like following: |
| 31 | + |
| 32 | +```php |
| 33 | +use yii\httpclient\Client; |
| 34 | + |
| 35 | +$client = new Client(['baseUrl' => 'http://domain.com/api/1.0']); |
| 36 | + |
| 37 | +$newUserResponse = $client->post('users', ['name' => 'John Doe', 'email' => 'johndoe@domain.com'])->send(); |
| 38 | +$articleResponse = $client->get('articles', ['name' => 'Yii 2.0'])->send(); |
| 39 | +$client->post('subscriptions', ['user_id' => $newUserResponse->data['id'], 'article_id' => $articleResponse->data['id']])->send(); |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +## Using different content formats |
| 44 | + |
| 45 | +By default HTTP request data is send as 'form-urlencoded', e.g. `param1=value1¶m2=value2`. |
| 46 | +This is a common format for the web forms, but not for the REST API, which usually demands content |
| 47 | +should be in JSON or XML format. You may setup format being used for request content using `format` |
| 48 | +property or `setFormat()` method |
| 49 | +Following formats are supported: |
| 50 | + |
| 51 | + - [[\yii\httpclient\Client::FORMAT_JSON]] - JSON format |
| 52 | + - [[\yii\httpclient\Client::FORMAT_URLENCODED]] - urlencoded by RFC1738 query string |
| 53 | + - [[\yii\httpclient\Client::FORMAT_RAW_URLENCODED]] - urlencoded by PHP_QUERY_RFC3986 query string |
| 54 | + - [[\yii\httpclient\Client::FORMAT_XML]] - XML format |
| 55 | + |
| 56 | +For example: |
| 57 | + |
| 58 | +```php |
| 59 | +use yii\httpclient\Client; |
| 60 | + |
| 61 | +$client = new Client(['baseUrl' => 'http://domain.com/api/1.0']); |
| 62 | +$response = $client->createRequest() |
| 63 | + ->setFormat(Client::FORMAT_JSON) |
| 64 | + ->setUrl('articles/search') |
| 65 | + ->setData([ |
| 66 | + 'query_string' => 'Yii', |
| 67 | + 'filter' => [ |
| 68 | + 'date' => ['>' => '2015-08-01'] |
| 69 | + ], |
| 70 | + ]) |
| 71 | + ->send(); |
| 72 | +``` |
| 73 | + |
| 74 | +The response object detects content format automatically based on 'Content-Type' header and content itself. |
| 75 | +So in most cases you don't need to specify format for response, you can parse it simply using `getData()` |
| 76 | +method or `data` property. In continue the above example we can get response data in following way: |
| 77 | + |
| 78 | +```php |
| 79 | +$responseData = $response->getData(); // get all articles |
| 80 | +count($response->data) // count articles |
| 81 | +$article = $response->data[0] // get first article |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +## Working with raw content |
| 86 | + |
| 87 | +No one is forcing you to rely on the built-in formats. You can specify raw content for your HTTP request |
| 88 | +as well as you can process a raw content of the response. For example: |
| 89 | + |
| 90 | +```php |
| 91 | +use yii\httpclient\Client; |
| 92 | + |
| 93 | +$client = new Client(['baseUrl' => 'http://domain.com/api/1.0']); |
| 94 | +$response = $client->createRequest() |
| 95 | + ->setUrl('articles/search') |
| 96 | + ->addHeaders(['content-type' => 'application/json']) |
| 97 | + ->setContent('{query_string: "Yii"}') |
| 98 | + ->send(); |
| 99 | + |
| 100 | +echo 'Search results:<br>'; |
| 101 | +echo $response->content; |
| 102 | +``` |
| 103 | + |
| 104 | +[[\yii\httpclient\Request]] formats specified `data` only if `content` is not set. |
| 105 | +[[\yii\httpclient\Response]] parses the `content` only if `data` is requested. |
| 106 | + |
| 107 | + |
| 108 | +## Pre-configure request and response objects |
| 109 | + |
| 110 | +If you are using single instance of [[\yii\httpclient\Client]] for several similar requests, |
| 111 | +for example, while working with REST API, you may simplify and speed up your code declaring |
| 112 | +your own configuration for request and response objects. This can be done via `requestConfig` |
| 113 | +and `responseConfig` fields of [[\yii\httpclient\Client]]. |
| 114 | +For example: you may want to setup JSON format for all request, created by particular client: |
| 115 | + |
| 116 | +```php |
| 117 | +use yii\httpclient\Client; |
| 118 | + |
| 119 | +$client = new Client([ |
| 120 | + 'baseUrl' => 'http://domain.com/api/1.0', |
| 121 | + 'requestConfig' => [ |
| 122 | + 'format' => Client::FORMAT_JSON |
| 123 | + ], |
| 124 | + 'responseConfig' => [ |
| 125 | + 'format' => Client::FORMAT_JSON |
| 126 | + ], |
| 127 | +]); |
| 128 | + |
| 129 | +$request = $client->createRequest(); |
| 130 | +echo $request->format; // outputs: 'json' |
| 131 | +``` |
| 132 | + |
| 133 | +> Tip: you may even specify your own classes for the request and response objects to facilitate |
| 134 | + some extra functionality you need, using 'class' key in configuration array. |
| 135 | + |
| 136 | + |
| 137 | +## Working with headers |
| 138 | + |
| 139 | +You may specify request headers using `setHeaders()` and `addHeaders()` methods. |
| 140 | +You may use `getHeaders()` method or `headers` property to get already defined headers as |
| 141 | +[[\yii\web\HeaderCollection]] instance. For example: |
| 142 | + |
| 143 | +```php |
| 144 | +use yii\httpclient\Client; |
| 145 | + |
| 146 | +$client = new Client(['baseUrl' => 'http://domain.com/api/1.0']); |
| 147 | +$request = $client->createRequest() |
| 148 | + ->setHeaders(['content-type' => 'application/json']) |
| 149 | + ->addHeaders(['user-agent' => 'My User Agent']); |
| 150 | + |
| 151 | +$request->getHeaders()->add('accept-language', 'en-US;en'); |
| 152 | +$request->headers->set('user-agent', 'User agent override'); |
| 153 | +``` |
| 154 | + |
| 155 | +Once you have a response object you can access all response headers using `getHeaders()` method |
| 156 | +or `headers` property: |
| 157 | + |
| 158 | +```php |
| 159 | +$response = $request->send(); |
| 160 | +echo $response->getHeaders()->get('content-type'); |
| 161 | +echo $response->headers->get('content-encoding'); |
| 162 | +``` |
| 163 | + |
| 164 | + |
| 165 | +## Working with cookies |
| 166 | + |
| 167 | +Although Cookies are transferred just as header values, [[\yii\httpclient\Request]] and [[\yii\httpclient\Request]] |
| 168 | +provides separated interface to work with them using [[\yii\web\Cookie]] and [[\yii\web\CookieCollection]]. |
| 169 | + |
| 170 | +You may specify request Cookies using `setCookies()` or `addCookies()` methods. |
| 171 | +You may use `getCookies()` method or `headers` property to get already defined Cookies as |
| 172 | +[[\yii\web\CookieCollection]] instance. For example: |
| 173 | + |
| 174 | +```php |
| 175 | +use yii\httpclient\Client; |
| 176 | +use yii\web\Cookie; |
| 177 | + |
| 178 | +$client = new Client(['baseUrl' => 'http://domain.com/api/1.0']); |
| 179 | +$request = $client->createRequest() |
| 180 | + ->setCookies([ |
| 181 | + ['name' => 'country', 'value' => 'USA'], |
| 182 | + new Cookie(['name' => 'language', 'value' => 'en-US']), |
| 183 | + ]) |
| 184 | + ->addCookies([ |
| 185 | + ['name' => 'view_mode', 'value' => 'full'] |
| 186 | + ]); |
| 187 | + |
| 188 | +$request->cookies->add(['name' => 'display-notification', 'value' => '0']); |
| 189 | +``` |
| 190 | + |
| 191 | +Once you have a response object you can access all response Cookies using `getCookies()` method |
| 192 | +or `cookies` property: |
| 193 | + |
| 194 | +```php |
| 195 | +$response = $request->send(); |
| 196 | +echo $response->getCookies()->get('country'); |
| 197 | +echo $response->headers->get('PHPSESSID'); |
| 198 | +``` |
| 199 | + |
| 200 | +You may transfer Cookies from response object to request using simple copy. |
| 201 | +For example: assume we need to edit user profile at some web application, which is available only |
| 202 | +after login, so we need to log in first and use created session for further activity: |
| 203 | + |
| 204 | +```php |
| 205 | +use yii\httpclient\Client; |
| 206 | + |
| 207 | +$client = new Client(['baseUrl' => 'http://domain.com']); |
| 208 | + |
| 209 | +$loginResponse = $client->post('login', [ |
| 210 | + 'username' => 'johndoe', |
| 211 | + 'password' => 'somepassword', |
| 212 | +])->send(); |
| 213 | + |
| 214 | +// $loginResponse->cookies->get('PHPSESSID') - holds the new session ID |
| 215 | + |
| 216 | +$client->post('account/profile', ['birthDate' => '10/11/1982']) |
| 217 | + ->setCookies($loginResponse->cookies) // transfer response cookies to request |
| 218 | + ->send(); |
| 219 | +``` |
0 commit comments