faiznurullah / rajaongkir
package for php application for connect with rajaongkir service
dev-main
2026-03-27 00:42 UTC
Requires
- guzzlehttp/guzzle: ^7.0
- vlucas/phpdotenv: ^5.6
README
A lightweight PHP library to access RajaOngkir endpoints through https://rajaongkir.komerce.id/api/v1.
Features
- Get provinces
- Get cities by province
- Get districts by city
- Get subdistricts by district
- Calculate domestic shipping cost by district
- Track waybill (AWB)
- Search domestic destinations
Requirements
- PHP 7.4+
- Composer
Installation
If you are using this package in another project:
composer require faiznurullah/rajaongkir
For local development in this repository:
composer install
Quick Start
<?php require_once __DIR__ . '/vendor/autoload.php'; use Faiznurullah\Rajaongkir\RajaOngkir; $apiKey = 'YOUR_API_KEY'; $rajaOngkir = new RajaOngkir($apiKey); $provinces = $rajaOngkir->getProvinces(); print_r($provinces);
API Methods
1) Get Provinces
$response = $rajaOngkir->getProvinces();
2) Get Cities
$provinceId = 5; $response = $rajaOngkir->getCities($provinceId);
3) Get Districts
$cityId = 39; $response = $rajaOngkir->getDistrict($cityId);
4) Get Subdistricts
$districtId = 574; $response = $rajaOngkir->getSubdistrict($districtId);
5) Calculate District Shipping Cost
$originDistrictId = 574; $destinationDistrictId = 114; $weight = 1000; // grams $courier = 'jne'; $response = $rajaOngkir->calculateDistrictCost( $originDistrictId, $destinationDistrictId, $weight, $courier );
6) Track Waybill
$waybill = 'JP1234567890'; $courier = 'jne'; $response = $rajaOngkir->trackWaybill($waybill, $courier);
7) Search Domestic Destination
$query = 'Bandung'; // With optional limit and offset $response = $rajaOngkir->searchDomesticDestination($query, 10, 0); // Without optional arguments $response = $rajaOngkir->searchDomesticDestination($query);
Error Handling
All methods return an array.
If a request fails, the library returns an array with an error description (from Guzzle exception or generic exception), for example:
[ 'rajaongkir' => [ 'status' => [ 'code' => 500, 'description' => 'Error: ...' ] ] ]
Run Local Test Script
This repository includes src/test.php.
From project root:
php src/test.php
Or from src directory:
php test.php
Troubleshooting
Class not found (RajaOngkir)
Make sure Composer autoload is loaded before using the class:
require_once __DIR__ . '/../vendor/autoload.php';
Warning: Module "ftp" is already loaded
This warning comes from your PHP extension configuration and is unrelated to this package.
Check your php.ini and remove duplicate extension=ftp entries.
License
MIT