หน้าหลัก » ภาษาพีเอชพี (PHP) » ตัวอย่างโค้ด PHP สำหรับใช้งาน API ของไปรษณีย์ไทย

ตัวอย่างโค้ด PHP สำหรับใช้งาน API ของไปรษณีย์ไทย




ตามที่ไปรษณีย์ไทย ได้มีการอัพเดทระบบติดตามสถานะสิ่งของสำหรับผู้ใช้ทั่วไป และเพิ่มในส่วนของ API สำหรับนักพัฒนา เพื่อที่จะสามารถพัฒนาต่อยอดได้อย่างหลากหลาย

1.เราจะต้องทำการสมัครสมาชิกก่อนจากลิงก์นี้

 https://track.thailandpost.co.th/register  

โดยในเบื้องต้นให้เลือกเป็นแบบ ลูกค้าทั่วไป ก่อน และทำตามขั้นตอนจนเสร็จ

API Thailandpost

2.เมื่อทำการสมัครสมาชิกเสร็จแล้วจะเข้าระบบหลังบ้านได้ เพื่อไปสร้าง Token Key สำหรับใช้งาน API โดยเลือกเมนู สำหรับนักพัฒนา > สำหรับนักพัฒนา > Create token > สร้าง จากนั้นก็จะได้ Token

API thailandpost

3. เมื่อได้ Token มาแล้วก็มาเขียนโค้ด PHP

4.สร้าง function สำหรับเรียกใช้งาน API ของไปรษณีย์ไทย

function api_request($url, $token, $content = null){
	
	$headers = [
        'Authorization: Token '. $token,
        'Content-Type: application/json'
    ];
	
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($content) );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec( $ch );
    curl_close($ch);
      
    return json_decode($result, true);	
}

5.กำหนดตัวแปรเก็บค่าเบื้องต้น

$api_token_url = 'https://trackapi.thailandpost.co.th/post/api/v1/authenticate/token';
$api_track_url = 'https://trackapi.thailandpost.co.th/post/api/v1/track';
$token_key     = '[เอามาจากข้อ 2 หมายเลข 5]';

6.กำหนดหมายเลขพัสดุตามที่ต้องการ หากมีหลายๆหมายเลขก็ คั่นด้วย , (comma)

$items = [
	'status' => 'all',
    'language' => 'TH',
    'barcode' => [
        '[หมายเลขพัสดุ]'
    ]
];

7.จะต้อง Authen เพื่อที่จะได้ access token

//Step1: GetToken()
$res_token = api_request($api_token_url, $token_key);

8.จากนั้นจะนำ Access Token ที่ได้มาทำต่อใน Step2 เพื่อดึงข้อมูลที่ได้ระบุไว้ในข้อ 6 มาแสดงผล

//Step2: GetItems()
$res_items = api_request($api_track_url, $res_token['token'], $items);

9.เมื่อระบบทำงานสำเร็จ เราก็จะนำค่าในตัวแปร $res_items มาใช้งานได้เลย หากอยากรู้ว่า $res_items มีอะไรบ้างให้ print_r( $res_items ); ออกมาดูนะครับ

โค้ด PHP สำหรับเชื่อมต่อ API ไปรษณีย์ไทย

<?php
/***************************************************
 * Author    : CS Developers
 * Author URI: https://www.comscidev.com
 * Facebook  : https://www.facebook.com/CSDevelopers
 ***************************************************/

$api_token_url = 'https://trackapi.thailandpost.co.th/post/api/v1/authenticate/token';
$api_track_url = 'https://trackapi.thailandpost.co.th/post/api/v1/track';
$token_key = 'E9RUUgRoSHP~K.....';

function api_request($url, $token, $content = null){
	
	$headers = [
        'Authorization: Token '. $token,
        'Content-Type: application/json'
    ];
	
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($content) );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec( $ch );
    curl_close($ch);
      
    return json_decode($result, true);	
}

//Items
$items = [
	'status' => 'all',
    'language' => 'TH',
    'barcode' => [
        'ED852942182TH'
    ]
];

//Step1: GetToken()
$res_token = api_request($api_token_url, $token_key);


//Step2: GetItems()
$res_items = api_request($api_track_url, $res_token['token'], $items);

print_r($res_items);

กรณีมีเลขพัสดุหลายๆ หมายเลข ให้แก้ไข $items ประมาณนี้

$items = [
    'status' => 'all',
    'language' => 'TH',
    'barcode' => [
        'ED852942182TH', 'ED852942182TH', 'ED852942182TH', 'ED852942182TH'
    ]
];

สามารถศึกษารายละเอียดเพิ่มเติมได้จาก https://track.thailandpost.co.th/developerGuide