Legacy mode
When creating an object for http-client, you can pass options as an array to the constructor (majority of options also already have setters):
Option | Type | Description |
---|---|---|
redirect * | bool | Enable redirect (true true by default – redirect). |
redirectMax * | int | Max. number of redirects (5 by default). |
waitResponse | bool | Standby for response or disconnect immediately after query (true by default – wait for response). |
socketTimeout | int | Connection timeout in seconds (default value: 30 ). |
streamTimeout | int | Stream timeout in seconds (default value: 60 ). |
version * | string | HTTP version (HttpClient::HTTP_1_0, HttpClient::HTTP_1_1) (default value: "1.0" ). |
proxyHost | string | Proxy server Name\URL. |
proxyPort | int | Proxy server port. |
proxyUser | string | Proxy server user name. |
proxyPassword | string | Proxy server password. |
compress | bool | Use gzip compression (default value: false ). |
charset | string | Encoding for contents of POST and PUT queries. |
disableSslVerification | bool | If true , no verification for SSL certificates is performed. |
bodyLengthMax | int | Maximum query length. |
privateIp | bool | Enable queries to private IP-addresses (default value: true – enable). |
debugLevel | int | Debugging level with constants HttpDebug::* . |
cookies * | array | Array with cookie files for a HTTP query. |
headers * | array | Array with titles for HTTP query. |
useCurl | bool | Enable CURL library (default value: false ). |
curlLogFile | string | Full path to file with CURL logs. |
* – options, operating only in legacy mode.
Option values by default at a specific project can be defined in settings.php:
return [ // ... "http_client_options" => [ "value" => [ "socketTimeout" => 20, "streamTimeout" => 20, "useCurl" => true, ], ] // ... ];
Main available methods:
Simple example:
use Bitrix\Main\Web\HttpClient; $http = new HttpClient([ 'compress' => true, 'headers' => [ 'User-Agent' => 'bitrix', ], ]); $result = $http->get('https://1c-bitrix.ru/'); if ($result !== false) { var_dump($http->getStatus()); var_dump($http->getHeaders()); } else { var_dump($http->getError()); }
Client will independently execute a redirect and will unpack a response.
The main 23.300.0 now has option to dynamically (contrary to the option waitResponse) define fetching for response body via callback function shouldFetchBody. Parameters for callback function receive response object with titles and a query object:
use Bitrix\Main\Web\HttpClient; use Bitrix\Main\Web\Http\Response; use Psr\Http\Message\RequestInterface; $http = new HttpClient(); $http->shouldFetchBody(function (Response $response, RequestInterface $request) { var_dump($request->getHeaders()); return ($response->getHeadersCollection()->getContentType() === 'text/html'); }); $result = $http->get('https://www.bitrix24.com/'); var_dump($result);
© «Bitrix24», 2001-2024