PHP code example of codesaur / http-client
1. Go to this page and download the library: Download codesaur/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/ */
codesaur / http-client example snippets
use codesaur\Http\Client\CurlClient;
// CurlClient үүсгэх / Create CurlClient instance
$curl = new CurlClient();
// GET хүсэлт илгээх / Send GET request
$response = $curl->request(
'https://httpbin.org/get',
'GET'
);
// Response обьект авах / Get Response object
$res = $curl->send('https://httpbin.org/get');
echo $res->statusCode; // 200
echo $res->getHeader('Content-Type'); // application/json
print_r($res->json()); // decoded JSON array
// Файл upload хийх / Upload file
$res = $curl->upload('https://httpbin.org/post', '/path/to/file.pdf');
// Дахин оролдох / Retry on failure
$res = $curl->sendWithRetry('https://httpbin.org/get', retries: 3);
// Debug горим / Debug mode
$curl->enableDebug(true);
$curl->send('https://httpbin.org/get');
print_r($curl->getDebugLog());
// Хариуг хэвлэх / Print response
echo $response;
use codesaur\Http\Client\JSONClient;
// JSONClient үүсгэх / Create JSONClient instance
$client = new JSONClient();
// GET хүсэлт илгээх / Send GET request
$response = $client->get(
'https://httpbin.org/get',
['hello' => 'world']
);
// POST хүсэлт илгээх / Send POST request
$response = $client->post(
'https://httpbin.org/post',
['test' => 'codesaur']
);
// HTTP/1.1 хувилбар ашиглах (HTTP/2 алдаанаас сэргийлэх)
// Use HTTP/1.1 version (to prevent HTTP/2 errors)
$response = $client->post(
'https://api.example.com/endpoint',
['data' => 'value'],
['Authorization' => 'Bearer token'],
[CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1]
);
// Base URL ашиглах / Use Base URL
$api = new JSONClient('https://api.example.com/v1');
$users = $api->get('/users');
// PATCH хүсэлт / PATCH request (partial update)
$response = $client->patch(
'https://httpbin.org/patch',
['status' => 'active']
);
// Хариуг хэвлэх / Print response
print_r($response);
use codesaur\Http\Client\Mail;
// Mail үүсгэх / Create Mail instance
$mail = new Mail();
// Хүлээн авагч тохируулах / Set recipient
$mail->targetTo('[email protected] ', 'Хэрэглэгч');
// Илгээгч тохируулах / Set sender
$mail->setFrom('[email protected] ', 'codesaur');
// Гарчиг тохируулах / Set subject
$mail->setSubject('Сайн байна уу?');
// Зурвас тохируулах / Set message
$mail->setMessage('<h1>Hello!</h1><p>Тест имэйл.</p>');
// Файл хавсралт нэмэх / Add file attachment
$mail->addFileAttachment(__DIR__ . '/file.pdf');
// URL-аас хавсралт нэмэх / Add attachment from URL
$mail->addUrlAttachment('https://example.com/logo.png');
// Имэйл илгээх / Send email
$mail->sendMail();