PHP code example of tims / shiprocket-php-sdk

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

    

tims / shiprocket-php-sdk example snippets


use Tims\Shiprocket\ShiprocketClient;

$shiprocket = ShiprocketClient::fromCredentials(
    getenv('SHIPROCKET_EMAIL'),
    getenv('SHIPROCKET_PASSWORD'),
);

use Tims\Shiprocket\AccessTokenGenerator;
use Tims\Shiprocket\ApiClient;
use Tims\Shiprocket\ShiprocketClient;

$token = (new AccessTokenGenerator())
    ->credentials($email, $password)
    ->getAccessToken();

$api = new ApiClient();
$api->setAccessToken($token);

$shiprocket = new ShiprocketClient($api);

use Tims\Shiprocket\ShiprocketClient;

$sr = ShiprocketClient::fromCredentials($email, $password);

$rates = $sr->couriers()->serviceability([
    'pickup_postcode' => '110030',
    'delivery_postcode' => '122001',
    'weight' => 0.5,
    'cod' => 0,
]);

$order = $sr->orders()->createAdhoc([
    'order_id' => 'ORD-1001',
    'order_date' => date('Y-m-d'),
    'pickup_location' => 'Primary',
    'billing_customer_name' => 'Jane',
    'billing_last_name' => 'Doe',
    'billing_address' => '221B Baker Street',
    'billing_city' => 'Mumbai',
    'billing_pincode' => '400001',
    'billing_state' => 'Maharashtra',
    'billing_country' => 'India',
    'billing_email' => '[email protected]',
    'billing_phone' => '9999999999',
    'shipping_is_billing' => true,
    'payment_method' => 'Prepaid',
    'sub_total' => 499,
    'length' => 10,
    'breadth' => 10,
    'height' => 10,
    'weight' => 0.5,
    'order_items' => [
        [
            'name' => 'Widget',
            'sku' => 'W-1',
            'units' => 1,
            'selling_price' => 499,
        ],
    ],
]);

$awb = $sr->couriers()->assignAwb([
    'shipment_id' => $order['shipment_id'],
]);
$sr->couriers()->generatePickup(['shipment_id' => [$order['shipment_id']]]);
$tracking = $sr->couriers()->trackByAwb($awb['response']['data']['awb_code'] ?? '');

$sr->apiClient()->invoke('GET', 'orders', ['page' => 1]);
$sr->apiClient()->invoke('GET', '/v1/warehouse/srf-serviceability', ['postcode' => '110030']);

use Tims\Shiprocket\ApiException;

try {
    $sr->orders()->show(123);
} catch (ApiException $e) {
    $e->getStatusCode();
    $e->getResponseBody();
    $e->getResponseHeaders();
}
bash
composer