Thango Ö
Thango Ö

Reputation: 11

is that possible sending http request from lumen using guzzlehttp?

actually i want to validate given token .. the validate code written in another lumen package. i have got some issues when i send request to validate token. i dont know why it's not working. cant use another api inside lumen ?

if i use that check token api in postman https://i.sstatic.net/kSpJt.png. it works fine. it's thrown error when i call that api inside other lumen package

this is what i got when use this api in postman https://i.sstatic.net/hfFzk.png

error log https://i.sstatic.net/ds0oR.png

<?php

namespace App\Helpers;

use App\Helpers\ResponseBuilder;

class check_customer_token_verification 
{
    public static function check($token, $vendor_id) 
    { 
        $client = new \GuzzleHttp\Client();
            $result = $client->post('dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token', [
                'form_params' => [
                    'token' => isset($token)?$token:'',
                    'vendor_id' => $vendor_id,
                ]
            ]); 

        $res_data = json_decode($result->getBody()->getContents()); dd($res_data);
            if ($res_data->http_code == 401) {
                return ResponseBuilder::responseResult(400, $res_data->message);
            }
        return $res_data;
    }
} ```

dump this api in postman. i got issue which is below


^ {#120
  +"http_code": 400
  +"message": """
    Server error: `POST dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token` resulted in a `500 Internal Server Error` response:
    <!DOCTYPE html>
    <html>
        <head>
            <meta name="robots" content="noindex,nofollow" />
            <style>
            (truncated...)
    """
}```



   


 


  

Upvotes: 1

Views: 2413

Answers (1)

bhucho
bhucho

Reputation: 3420

You need to define your form_params, though your code can work but I would also suggest using try catch blocks and adding your 401 exception in catch block (& other 400 in there), see the changes I have made

 public static function check($token, $vendor_id) 
    { 
        try{
            $client = new \GuzzleHttp\Client();
            define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
            $guzzleResponse = $client->post('dev.adiswar-crm.local/customer/crm/v-1-0-0/check-token', [
                'form_params' => [
                    'token' => isset($token) && !empty($token) ? $token : '',
                    'vendor_id' => $vendor_id,
                ]
            ]); 
            if ($guzzleResponse->getStatusCode() == 200) {
               $result = json_decode($guzzleResponse->getBody(),true);
               // dd($result);
            }
            
            return $result;
        }catch(\GuzzleHttp\Exception\RequestException $e){
           // Catch all 4XX errors 
           dd($e->getMessage, $e->getTraceAsString());
           // To catch exactly error 401 use 
           if ($e->hasResponse()){
               if ($e->getResponse()->getStatusCode() == '401') {
                   return ResponseBuilder::responseResult(400, $e->getMessage());
            }
         } 
        }catch(Exception $e){
           //other errors 
        }
    }

Upvotes: 0

Related Questions