PHP code example of olegv9 / http-client
1. Go to this page and download the library: Download olegv9/http-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
olegv9 / http-client example snippets
use HttpClient\HttpClient;
$http = new HttpClient();
$url = 'https://example.com';
$response = $http->get($url)->text();
$response = $http->post($url, ['some' => 'data'])->text();
$response = $http->put($url, ['some' => 'data'])->text();
$response = $http->delete($url, ['some' => 'data'])->text();
$text = $http->get($url)->text(); //raw text
$object = $http->get($url)->json(); //json_decode() applied
$array = $http->get($url)->json(true); //json_decode(true) applied
$res = $http->get($url);
$res->text(); //see above
$res->json(); //see above
//status code (200, 403, etc)
$httpCode = $res->code();
//sending request error (network failure, invalid URL, etc)
$error = $res->error();
//last URL after redirect chain (CURLINFO_EFFECTIVE_URL)
$lastUrl = $res->getUrl();
//response header
$header = $res->getHeader('Content-Type');
//all response headers list
$allHeaders = $res->getAllHeaders();
//request time
$time = $res->time();
//авторизация + заголовки
$opts = [
'auth' => 'admin:NgjrP4n',
'headers' => [
'X-Some-Header' => 'OK'
]
];
$http->get($url, $opts);
//Отправка JSON'а
$data = [
'site' => 'somesite.com',
'se' => 197,
'prim' => true
];
$opts = [
'timeout' => 10,
'type' => 'json'
];
$http->post($url, $data, $opts);
$urls = [
'http://example.com',
'http://example.com/page',
'http://example.org'
];
$opts = ['timeout' => 10];
$results = $http->multiGet($urls, $opts);
//или
//$results = $http->multiRequest('POST', $urls, $postData, $opts);
foreach ($results as $res) {
echo $res->text(); //текст ответа. Доступные также все методы, описанные выше
}
$urls = [
'http://example.com',
'http://example.com/send',
[
'method' => 'POST',
'url' => 'http://example.net',
'data' => ['a' => 123],
'opts' => ['timeout' => 20]
],
'http://example.org'
];
$http->multiRequest('GET', $urls);