Shiprocket API implementation in PHP is so easy, Here we will use CodeIgniter Framework,
We will see two processes:
- Courier to Customer
To create order/Send a courier through API we have to request cURL “https://apiv2.shiprocket.in/v1/external/orders/create/adhoc” with “token“. - Pickup From Customer
To Create ‘Return’ through API we have to request cURL “https://apiv2.shiprocket.in/v1/external/orders/create/return” with “token“.
We will see how to create and send token in cURL below.
So let’s get started.
Create a Token in Shiprocket:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public function shiprocketAuth(){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apiv2.shiprocket.in/v1/external/auth/login', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{ "email": "info@codypaste.com", "password": "asdfgadf9876^%$" }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $tknresponse = curl_exec($curl); curl_close($curl); $tknres = json_decode($tknresponse, true); $tokenRes = $tknres['token']; return $tokenRes; } |
This method/Function will return Token, wiil use this token to ‘Create’ and ‘Pickup’ on both process.
Note: Please update CURLOPT_POSTFIELDS value email and password with your detail.
So, here you can see complete code example below.
===================================================
Controller
Create a file application/controllers/Shiprocket.php with given code below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Shiprocket extends CI_Controller { public function __construct(){ error_reporting(0); parent::__construct(); $this->load->library(array('form_validation','session','cart')); //$this->load->database(); } public function index(){ $this->load->view('shiprocketView'); } public function shiprocketAuth(){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apiv2.shiprocket.in/v1/external/auth/login', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{ "email": "info@codypaste.com", "password": "asdfgadf9876^%$" }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $tknresponse = curl_exec($curl); curl_close($curl); $tknres = json_decode($tknresponse, true); $tokenRes = $tknres['token']; return $tokenRes; } public function shiprocketSendToCustomer(){ $order_id = "CODYPASTE-01"; // Unique order id $order_date = "2022-12-14"; // Current date, YY-MM-DD $channel_id = "123456"; // From Shiprocket Panel, Login to shiprocket dashboard $billing_customer_name = "Vipul"; // Courier Receiver Name $billing_last_name = "Rai"; // Courier Receiver Last Name $billing_address = "401, Frinds Enclave"; // Courier Receiver $billing_address_2 = "Shahberi"; // Courier Receiver $billing_city = "Noida"; // Courier Receiver $billing_state = "Uttarpradesh"; // Courier Receiver $billing_pincode = "201009"; $billing_email = "info@asd.com"; $billing_phone = "9999999999"; $name = "Cody Paste T-Shirt"; // Product name $sku = "CPTS-01"; // Product SKU $units = "1"; // Number of product $selling_price = "450"; // Price of product $discount = "0"; // discount of product $payment_method = "Prepaid"; $sub_total = "450"; // Subtotal of product $length = "30"; // in centimeter $breadth = "25"; // in centimeter $height = "10"; // in centimeter $weight = "1.59"; //in Kilogram (KG) $tokenRes = $this->shiprocketAuth(); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apiv2.shiprocket.in/v1/external/orders/create/adhoc', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{ "order_id": "'.$order_id.'", "order_date": "'.$order_date.'", "pickup_location": "Primary", "channel_id": "'.$channel_id.'", "comment": "Cody Paste", "billing_customer_name": "'.$billing_customer_name.'", "billing_last_name": "'.$billing_last_name.'", "billing_address": "'.$billing_address.'", "billing_address_2": "'.$billing_address_2.'", "billing_city": "'.$billing_city.'", "billing_pincode": '.$billing_pincode.', "billing_state": "'.$billing_state.'", "billing_country": "India", "billing_email": "'.$billing_email.'", "billing_phone": "'.$billing_phone.'", "shipping_is_billing": true, "shipping_customer_name": "Cody Paste", "shipping_last_name": "", "shipping_address": "", "shipping_address_2": "", "shipping_city": "", "shipping_pincode": "", "shipping_country": "", "shipping_state": "", "shipping_email": "", "shipping_phone": "", "order_items": [ { "sku": "'.$sku.'", "name": "'.$name.'", "units": '.$units.', "selling_price": '.$selling_price.', "discount": '.$discount.', "qc_enable":false, "hsn": "", "brand":"", "qc_size":"" } ], "payment_method": "'.$payment_method.'", "shipping_charges": 0, "giftwrap_charges": 0, "transaction_charges": 0, "total_discount": 0, "sub_total": '.$sub_total.', "length": '.$length.', "breadth": '.$breadth.', "height": '.$height.', "weight": '.$weight.' }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: Bearer '.$tokenRes.'' ), )); $response = curl_exec($curl); curl_close($curl); } public function shiprocketPickupFromCustomer(){ $order_id = "CODYPASTE-02"; // Unique order id $order_date = "2022-12-14"; // Current date, YY-MM-DD $channel_id = "123456"; // From Shiprocket Panel, Login to shiprocket dashboard $pickup_customer_name = "Vipul"; // Customer name, from whre you want to pickup $pickup_last_name = "Rai"; // Customer $pickup_address = "401, Living Homes"; $pickup_address_2 = "Friends Enclave"; $pickup_city = "Noida"; $pickup_state = "Uttarpradesh"; $pickup_pincode = "201009"; $pickup_email = "vips.rai@gmail.com"; $pickup_phone = "9999999999"; $shipping_customer_name = "Cody Paste"; // Name of Courier Sender. $shipping_address = "345, Canaught Place"; $shipping_address_2 = "Kamala Market"; $shipping_city = "New Delhi"; $shipping_country = "India"; $shipping_pincode = "110033"; $shipping_state = "Delhi"; $shipping_email = "info@codypaste.com"; $shipping_phone = "8989899878"; $order_items = "/"; $name = "Cody Paste T-Shirt"; // Order Product Name $sku = "CPTS01"; // Product SKU $units = "1"; // Number of products $selling_price = "450"; // Price of product $discount = "0"; // discount of product $payment_method = "Prepaid"; $sub_total = "450"; // Subtotal of product $length = "30"; // in centimeter $breadth = "25"; // in centimeter $height = "10"; // in centimeter $weight = "1.59"; //in Kilogram (KG) $tokenRes = $this->shiprocketAuth(); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://apiv2.shiprocket.in/v1/external/orders/create/return', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{ "order_id": "'.$order_id.'", "order_date": "'.$order_date.'", "channel_id": "'.$channel_id.'", "pickup_customer_name": "'.$pickup_customer_name.'", "pickup_last_name": "'.$pickup_last_name.'", "company_name":"Cody Paste", "pickup_address": "'.$pickup_address.'", "pickup_address_2": "'.$pickup_address_2.'", "pickup_city": "'.$pickup_city.'", "pickup_state": "'.$pickup_state.'", "pickup_country": "India", "pickup_pincode": '.$pickup_pincode.', "pickup_email": "'.$pickup_email.'", "pickup_phone": "'.$pickup_phone.'", "pickup_isd_code": "91", "shipping_customer_name": "'.$shipping_customer_name.'", "shipping_last_name": "", "shipping_address": "'.$shipping_address.'", "shipping_address_2": "'.$shipping_address_2.'", "shipping_city": "'.$shipping_city.'", "shipping_country": "India", "shipping_pincode": '.$shipping_pincode.', "shipping_state": "'.$shipping_state.'", "shipping_email": "'.$shipping_email.'", "shipping_isd_code": "91", "shipping_phone": '.$shipping_phone.', "order_items": [ { "sku": "'.$sku.'", "name": "'.$name.'", "units": '.$units.', "selling_price": '.$selling_price.', "discount": '.$discount.', "qc_enable":false, "hsn": "", "brand":"", "qc_size":"" } ], "payment_method": "'.$payment_method.'", "total_discount": "0", "sub_total": '.$sub_total.', "length": '.$length.', "breadth": '.$breadth.', "height": '.$height.', "weight": '.$weight.' }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: Bearer '.$tokenRes.'' ), )); $response = curl_exec($curl); curl_close($curl); //echo $response; } } |
(Please go through every line of code to understand the variable values and other things)
View
Create a file application/views/shiprocketView.php with given code below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Shiprocket</title> </head> <body> <div id="container"> <form action="<?php echo base_url(); ?>shiprocket/shiprocketSendToCustomer" method="post"> <button type="submit" alue="Submit">Send to Customer</button> </form> <form action="<?php echo base_url(); ?>shiprocket/shiprocketPickupFromCustomer" method="post"> <button type="submit" alue="Submit">Pickup From Customer</button> </form> </div> </body> </html> |