PHP code example of farzai / thailand-post
1. Go to this page and download the library: Download farzai/thailand-post 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/ */
farzai / thailand-post example snippets
use Farzai\ThaiPost\ClientBuilder;
use Farzai\ThaiPost\Endpoints\ApiEndpoint;
// สร้างตัวเชื่อมต่อ api
// โดยใช้ ClientBuilder ที่เราสร้างขึ้นมา เพื่อใช้ในการตั้งค่าต่างๆ
$client = ClientBuilder::create()
// API Key ที่ได้มาจากการ generate ผ่านหน้าเว็บของไปรษณีย์ไทย
->setCredential('YOUR_API_KEY')
// (Optional) ตั้งค่าที่จัดเก็บ Token ที่ได้มาจากการเรียก API
// โดยท่านต้อง Implement \Farzai\ThaiPost\Contracts\StorageRepositoryInterface ให้เรียบร้อย
// ->setStorage(new YourStorageRepository())
// (Optional) ตั้งค่า Http Client ที่ท่านต้องการใช้งาน
// ->setHttpClient(new \GuzzleHttp\Client())
// (Optional) ตั้งค่า Logger ที่ท่านต้องการใช้งาน
// ->setLogger(new \Monolog\Logger('thai-post'))
// Build ตัวเชื่อมต่อ api
->build();
// เรียกใช้งานตัวเชื่อมต่อ api
$api = new ApiEndpoint($client);
try {
// ส่งคำร้องขอเรื่อง ดึงสถานะของ barcode
$response = $api->getItemsByBarcodes([
'barcode' => ['EY145587896TH', 'RC338848854TH'],
]);
} catch (InvalidApiTokenException $e) {
// กรณีที่ API Token ไม่ถูกต้อง
exit($e->getMessage());
}
// คุณสามารถนำ json response มาใช้งานได้จากคำสั่งด้านล่างได้เลย
$array = $response->json();
// หรือ ต้องการเข้าไปยัง path ของ json
$countNumber = $response->json('response.track_count.count_number');
$response = $api->getItemsByBarcodes([
'barcode' => ['EY145587896TH', 'RC338848854TH'],
// Options
'status' => 'all',
'language' => 'TH',
]);
$response = $api->getItemsByReceipts([
'receiptNo' => ['RC338848854TH'],
// Options
'status' => 'all',
'language' => 'TH',
]);
$response = $api->generateAccessToken();
use Farzai\ThaiPost\ClientBuilder;
use Farzai\ThaiPost\Endpoints\WebhookEndpoint;
$client = ClientBuilder::create()
->setCredential('YOUR_API_KEY')
->build();
$webhook = new WebhookEndpoint($client);
$response = $webhook->subscribeBarcodes([
'barcode' => ['EY145587896TH', 'RC338848854TH'],
]);
// ตรวจสอบว่าทำงานถูกต้องหรือไม่
if ($response->isSuccessfull() && $response->json('status') === true) {
$returnedJson = $response->json();
// Or
$message = $response->json('message');
$items = $response->json('response.items');
$trackCount = $response->json('response.track_count.count_number');
}
$response = $webhook->subscribeBarcodes([
'barcode' => ['EY145587896TH', 'RC338848854TH'],
// Options
'status' => 'all',
'language' => 'TH',
'req_previous_status' => true,
]);
$response = $webhook->subscribeReceipts([
'receiptNo' => ['RC338848854TH'],
// Options
'status' => 'all',
'language' => 'TH',
'req_previous_status' => true,
]);
$response = $webhook->subscribeByProfile([
'fullName' => 'John Doe',
'telephone' => '0123456789',
// Options
'email' => '[email protected] ',
'nickname' => 'John',
]);
$response = $webhook->unsubscribeByProfile([
'uid' => '1234567890',
'ref' => '1234567890',
]);