PHP code example of rusdyahmad / php-easyparcel

1. Go to this page and download the library: Download rusdyahmad/php-easyparcel 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/ */

    

rusdyahmad / php-easyparcel example snippets


use PhpEasyParcel\EasyParcel;

// Initialize with API key and country code
$easyparcel = new EasyParcel('your-api-key', 'my'); // 'my' for Malaysia, 'sg' for Singapore, etc.

use PhpEasyParcel\Config;
use PhpEasyParcel\EasyParcel;

// Load environment variables
Config::loadEnv();

// Initialize using environment variables
$easyparcel = new EasyParcel();

try {
    $response = $easyparcel->checkBalance();
    
    if (isset($response['error_code']) && $response['error_code'] === '0') {
        echo "Credit Balance: " . $response['result'];
    } else {
        echo "Error: " . $response['error_remark'];
    }
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

try {
    $shipment = [
        'pick_code' => '50000',
        'pick_state' => 'Kuala Lumpur',
        'pick_country' => 'my',
        'send_code' => '11950',
        'send_state' => 'Penang',
        'send_country' => 'my',
        'weight' => 1.0,
        'width' => 10,
        'height' => 10,
        'length' => 10,
    ];
    
    $response = $easyparcel->getRates($shipment);
    
    if (isset($response['error_code']) && $response['error_code'] === '0') {
        $rates = $response['result'][0]['rates'];
        foreach ($rates as $rate) {
            echo $rate['service_name'] . ": " . $rate['price'] . "\n";
        }
    } else {
        echo "Error: " . $response['error_remark'];
    }
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

use PhpEasyParcel\ShipmentBuilder;

try {
    $shipment = ShipmentBuilder::create()
        ->from('John Doe', '0123456789', '123 Main Street', 'Kuala Lumpur', '50000', 'Kuala Lumpur', 'my')
        ->to('Jane Smith', '0123456789', '456 Second Street', 'Penang', '11950', 'Penang', 'my')
        ->withDimensions(1.0, 10, 10, 10)
        ->build();
    
    $response = $easyparcel->getRates($shipment);
    
    // Process response as above
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

use PhpEasyParcel\ShipmentBuilder;

try {
    $order = ShipmentBuilder::create()
        ->from('John Doe', '0123456789', '123 Main Street', 'Kuala Lumpur', '50000', 'Kuala Lumpur', 'my')
        ->fromCompany('ABC Company')
        ->to('Jane Smith', '0123456789', '456 Second Street', 'Penang', '11950', 'Penang', 'my')
        ->toEmail('[email protected]')
        ->withDimensions(1.0, 10, 10, 10)
        ->withContent('Books')
        ->withServiceId('EP-MY0003') // Service ID from rates response
        ->withCollectionDate('2025-03-25')
        ->withSmsNotification(true)
        ->withWhatsAppTracking(true)
        ->build();
    
    $response = $easyparcel->submitOrder($order);
    
    if (isset($response['error_code']) && $response['error_code'] === '0') {
        $orderDetails = $response['result'][0];
        echo "Order Number: " . $orderDetails['order_number'] . "\n";
        echo "Status: " . $orderDetails['parcel_status'] . "\n";
    } else {
        echo "Error: " . $response['error_remark'];
    }
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

try {
    $orderNo = 'EPC-123456789'; // Order number from submit order response
    
    $response = $easyparcel->payOrder($orderNo);
    
    if (isset($response['error_code']) && $response['error_code'] === '0') {
        $paymentDetails = $response['result'][0];
        echo "Order Number: " . $paymentDetails['order_number'] . "\n";
        echo "Status: " . $paymentDetails['parcel_status'] . "\n";
        echo "Tracking Number: " . $paymentDetails['tracking_number'] . "\n";
    } else {
        echo "Error: " . $response['error_remark'];
    }
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

use PhpEasyParcel\Response;

try {
    $shipment = ShipmentBuilder::create()
        ->from('John Doe', '0123456789', '123 Main Street', 'Kuala Lumpur', '50000', 'Kuala Lumpur', 'my')
        ->to('Jane Smith', '0123456789', '456 Second Street', 'Penang', '11950', 'Penang', 'my')
        ->withDimensions(1.0, 10, 10, 10)
        ->build();
    
    $rawResponse = $easyparcel->getRates($shipment);
    $response = new Response($rawResponse);
    
    if ($response->isSuccessful()) {
        $rates = $response->getRates();
        foreach ($rates as $rate) {
            echo $rate['service_name'] . ": " . $rate['price'] . "\n";
        }
    } else {
        echo "Error: " . $response->getErrorMessage();
    }
} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage();
}

// Production environment (default)
$productionClient = new EasyParcel('your-api-key', 'my');

// Sandbox environment
$sandboxOptions = [
    'base_uri' => "https://demo.connect.easyparcel.my/"
];
$sandboxClient = new EasyParcel('your-api-key', 'my', $sandboxOptions);

// Create a client (defaults to production)
$client = new EasyParcel('your-api-key', 'my');

// Switch to sandbox
$client->useSandbox();

// Switch back to production
$client->useProduction();

// Check current environment
echo $client->getBaseUrl();
bash
composer