Documentation

HttpClient constructor

public
\Bitrix\Main\Web\HttpClient::__construct(
 array $options = null
);

Non-static method is called when creating a class instance, allowing to perform specific actions within it when creating an object.

Parameters

ParameterDescriptionVersion
optionsArray with parameters:
  • redirect * (bool) - Follow the redirect (default value is true - follow the redirect).
  • redirectMax * (int) - Maximum number of redirects (default value: 5).
  • waitResponse (bool) - Response timeout or immediate disabling after the query (default value is true - await for redirect)
  • 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\address.
  • proxyPort (int) - Proxy server port.
  • proxyUser (string) - Proxy server user name.
  • proxyPassword (string) - Proxy password.
  • compress (bool) - Use of zip compression (default value: false). When true, sends Accept-Encoding: gzip.
  • charset (string) - Encoding for content of POST and PUT queries. (Used in the Content-Type query header.)
  • disableSslVerification (bool) - When true, verification of SSL certificates is disabled.
  • bodyLengthMax (int) - maximum query length;
  • privateIp (bool) - enable queries to frequent IP addresses (default: true – enabled);
  • debugLevel (int) - debugging level with constants, HttpDebug::*;
  • cookies * (array) - array with cookie files for HTTP query;
  • headers * (array) - array with headers for HTTP query;
  • useCurl (bool) - enable CURL (default: false);
  • curlLogFile (string) - full file path with CURL. logs
* – options, working only in legacy mode.

All these options are optional when creating a class instance, they can be configured later.

Example

Create a class instance:

$http=new \Bitrix\Main\Web\HttpClient(array $options = null);)

Execute GET query. Parameter requires absolute pathname. Returns a response string or false, when error occurs. Please, be advised, that empty string is not an error.

$http->get($url)

Execute POST query. First parameter contains absolute pathname. Second parameter must be an array, string, object. Similar to the previous method, returns a response string or false, when error occurs.

$http->post($url, $postData = null)

Specify http header in the query. First parameter sets header field name, second parameter indicates this field value. (Be advised, that in both cases, string is passed. When several headers are needed, the method must be called several times.) Third parameter is responsible for header rewriting when parameter with the same name already is specified.

$http->setHeader($name, $value, $replace = true)

Specifying cookies for query. The only parameter receives array with key as the cookies name and value is the cookies value.

$http->setCookies(array $cookies)

Specifying header field for basic http authorization query. User name and password are passed in parameters.

$http->setAuthorization($user, $pass)

Specifying forwarding options. First parameter enables forwarding and second parameter sets maximum number of redirects.

$http->setRedirect($value, $max = null)

Specifying parameter, that sets server's response timeout or connection closure immediately after query. Standby by default.

$http->waitResponse($value)

Specifying maximum response timeout in seconds.

$http->setTimeout($value)

Specifying HTTP protocol version. Default version is 1.0. Can be specified as 1.1

$http->setVersion($value)

Enables use of compression. Please note, the compressed response is processed in any case when headers are passed correctly.

$http->setCompress($value)

Specifying encoding for bodies of objects for POST and PUT queries.

$http->setCharset($value)

Specifying parameter for use proxy server. Specifies host or an address as the first parameter. Second parameter specifies port and third parameter - user name. Fourth parameter contain password. All parameters except host name are optional.

$http->setProxy($proxyHost, $proxyPort = null, $proxyUser = null, $proxyPassword = null)

Specifying the result as stream. Parameter passes a resource to file or to stream.

$http->setOutputStream($handler)

Downloading and saving the file. First parameter is the address used to download the file. Second parameter is the absolute pathname for saving the file.

$http->download($url, $filePath)


© «Bitrix24», 2001-2024