Documentation

HttpClient

HttpClient class handling HTTP. Class operates via sockets. Class gets default options from http_client_options configuration settings of the file /bitrix/.settings.php by default.

Starting from main 23.0.0 HttpClient now supports PSR-18. In addition to PSR, client works in legacy mode, supports queues with asynchronous queries and CURL.

Method Description Available from version
disableSslVerification Disables SSL certificate verification.
download Downloads and saves files.
get Executes GET query.
getCharset Returns response content encoding.
getContentType Returns type of response content.
getCookies Returns parsed HTTP cookies response.
getEffectiveUrl Returns last redirect URL or original URL.
getError Returns array of errors on failure.
getHeaders Returns parsed HTTP response headers.
getResult Returns string for HTTP response entity.
getStatus Returns status code for HTTP response.
head Executes HEAD query.
post Executes POST query.
query Executes HTTP query.
setAuthorization Sets title field for authentication query.
setCharset Sets encoding for the object body.
setCompress Sets compression options.
setCookies Sets array with cookies for HTTP query.
setHeader Sets header field for HTTP query.
setOutputStream Sets response output stream instead of string result.
setProxy Sets query HTTP proxy.
setRedirect Sets redirect options.
setStreamTimeout Sets socket stream for timeout reading.
setVersion Sets HTTP protocol version.
waitResponse Sets response waiting option.
destruct Closes connection when object is destroyed.
Constructor Creates new class object.
setTimeout Sets connection timeout.

Example

Setup example:

  'http_client_options' =>
   array (
     'value' =>
        array (
         'redirect' => true,//make redirects, if required
         'redirectMax' => 10,//no more than 10
         'version' => '1.1'//operate via protocol http 1.1
        ),
     'readonly' => false,
   ),

Setup accuracy can be checked as follows (indicate your own array):

use Bitrix\Main\Config\Configuration;
print_r(Configuration::getValue("http_client_options"));

use Bitrix\Main\Web\HttpClient;

$httpClient = new HttpClient();
$httpClient->setHeader('Content-Type', 'application/json', true);
$response = $httpClient->post('http://www.example.com', json_encode(array('x' => 1)));

use Bitrix\Main\Web\HttpClient;

$httpClient = new HttpClient();
$httpClient->download('http://www.example.com/robots.txt', $_SERVER['DOCUMENT_ROOT'].'/upload/my.txt');

use Bitrix\Main\Web\HttpClient; 
$url = "http://www.example.com"; // cookie sourced from here
$url2 = "http://www.example.com/form_request"; // send query to this address
$post = "val1=true&val2=false"; // string query, or an array

$httpClient = new HttpClient(); 
$httpClient->query("GET", $url);
$cookie = $httpClient->getCookies()->toArray(); // Cookie is passed by the object -> insert it into the array. 

echo "<pre>"; print_r($cookie); echo "</pre>";

$httpClient->setHeader('Content-Type','application/x-www-form-urlencoded'); 
$httpClient->setCookies($cookie); // Argument must be an array!
$response = $httpClient->post($url2, $post);

echo "<pre>"; var_dump($response); echo "</pre>";


© «Bitrix24», 2001-2024
Up