HTTP хүсэлт илгээх болон MIME имэйл боловсруулах/илгээх энгийн хөнгөн жинтэй, объект хандалтат http-client компонент.
- Монгол | 2. English | 3. Getting Started
codesaur/http-client нь codesaur ecosystem-ийн нэг хэсэг бөгөөд хөнгөн жинтэй,
фрэймворкоос үл хамааран standalone байдлаар ашиглаж болох PHP HTTP клиент компонент юм.
Багц нь дараах 4 үндсэн class-аас бүрдэнэ:
- CurlClient - cURL дээр суурилсан уян хатан HTTP клиент
- JSONClient - JSON өгөгдөлтэй REST API-тэй ажиллахад тохиромжтой
- Response - HTTP хариуг обьект хэлбэрээр илэрхийлэх (status code, headers, body)
- Mail - HTML + Text + олон хавсралттай MIME имэйл илгээгч
- UTF-8 бүрэн дэмжлэг (нэрс, файлын нэр, гарчиг г.м.)
- Хөнгөн, хурдан, ямар ч фрэймворк дээр эсвэл дангаар ашиглаж болно
- Зөвхөн
ext-curl,ext-jsonбайхад л болно
- Бүрэн танилцуулга - Суурилуулалт, хэрэглээ, жишээнүүд
- API тайлбар - Бүх метод, exception-үүдийн тайлбар
- Шалгалтын тайлан - Код шалгалтын тайлан
codesaur/http-client is part of the codesaur ecosystem and is a lightweight PHP HTTP client component that can be used standalone, independent of any framework.
The package consists of the following 4 core classes:
- CurlClient - flexible HTTP client based on cURL
- JSONClient - convenient for working with REST APIs with JSON data
- Response - HTTP response object with status code, headers, and body
- Mail - MIME email sender with HTML + Text + multiple attachments
- Full UTF-8 support (names, file names, headers, etc.)
- Lightweight, fast, can be used on any framework or standalone
- Only requires
ext-curlandext-json
- Full Documentation - Installation, usage, examples
- API Reference - Complete API documentation
- Review - Complete package review and code quality assessment
- PHP 8.2.1+
- Composer
ext-curlextensionext-jsonextension
Composer ашиглан суулгана / Install via Composer:
composer require codesaur/http-clientuse 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('user@example.com', 'Хэрэглэгч');
// Илгээгч тохируулах / Set sender
$mail->setFrom('no-reply@example.com', '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();Тест ажиллуулах / Run tests:
# Бүх тестүүдийг ажиллуулах / Run all tests
composer test
# Зөвхөн unit тест / Unit tests only
composer test:unit
# Зөвхөн integration тест / Integration tests only
composer test:integration
# Coverage-тэй тест ажиллуулах / Run tests with coverage
composer test:coverage- CHANGELOG.md - Full version history
This project is licensed under the MIT License.
Narankhuu
codesaur@gmail.com
https://github.com/codesaur
codesaur ecosystem: https://codesaur.net