Generating token with from Shiprocket using Laravel 9 and Guzzle Http, however GuzzleHttp\Psr7\Request has not been used. public static function generateShipRocketToken() { if(config()->get('shiprocket_email') == null) dd('Shiprocket email not et in DB'); $shiprocketEmailId = config()->get('shiprocket_email'); $shiprocketPassword = config()->get('shiprocket_password'); // Some test code here. Some more test here $last_rec = Setting::all()->last(); try { $client = new \GuzzleHttp\Client(); $headers = ["headers" => [ 'Content-Type' => 'application/json', ], 'body' => json_encode(['email'=>$shiprocketEmailId, 'password'=>$shiprocketPassword]), 'verify' => false, ]; if($last_rec->shiprocket_token == null) { //If token null $g_request = $client->requestAsync('POST', 'https://apiv2.shiprocket.in/v1/external/auth/login',$headers) ->wait(); $response = json_decode($g_request->getBody()); $token = $response->token; $last_rec->update([ 'shiprocket_token' => $token, 'shiprocket_updated_at' => Carbon::now()->format('Y-m-d h:i:s'), ]); } else { // $timeNow = Carbon::now(new \DateTimeZone('Asia/Kolkata')); //If token expired $timeNow = Carbon::now(); $lastTokenTime = Carbon::parse($last_rec->shiprocket_updated_at)->format('Y-m-d h:i:s'); $hoursDifference = $timeNow->diffInHours($lastTokenTime); $token = null; if($hoursDifference > 240) { //if token difference greater than 10 days // Create new token if token more than a day old $g_request = $client->requestAsync('POST', 'https://apiv2.shiprocket.in/v1/external/auth/login',$headers) ->wait(); $response = json_decode($g_request->getBody()); $token = $response->token; // Save record into the table $last_rec->update([ 'shiprocket_token' => $token, 'shiprocket_updated_at' => Carbon::now()->format('Y-m-d h:i:s'), ]); } else { // Get current token //If token not expired $token = $last_rec->shiprocket_token; }} }catch(GuzzleException $e1) { dd($e1->getMessage()); } return $token; }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners