Home > other >  How to convert PHP cURL format into Guzzle Laravel Format
How to convert PHP cURL format into Guzzle Laravel Format

Time:09-27

currently i'm trying to test some API endpoint use postman. As we know that postman provide us the information to integrate the request into the code in many different languages. So i select code format PHP-cURL on my laravel framework and it works. however i want to convert PHP-cURL format into laravel Guzzle. but didn't work.

here is the PHP-cURL code

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://sandbox.plaid.com/asset_report/create',
  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 =>'{
   "client_id": "xxxx",
   "secret": "xxxx",
   "access_tokens": ["access-sandbox-xxx"],
   "days_requested": 30,
   "options": {
      "client_report_id": "ENTER_CLIENT_REPORT_ID_HERE",
      "webhook": "https://www.example.com/webhook",
      "user": {
        "client_user_id": "ENTER_USER_ID_HERE",
        "first_name": "ENTER_FIRST_NAME_HERE",
        "middle_name": "ENTER_MIDDLE_NAME_HERE",
        "last_name": "ENTER_LAST_NAME_HERE",
        "ssn": "111-22-1234",
        "phone_number": "1-415-867-5309",
        "email": "ENTER_EMAIL_HERE"
      }
   }
 }',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

this PHP-cURL format can success run and display the response on my laravel. but when i change to Laravel Guzzle.. it display error. here is the guzzle code

use GuzzleHttp\Client;

$guzzle = new Client;
$getAccestToken = $guzzle->request('POST', 'https://sandbox.plaid.com/asset_report/create', [
    'headers' => ['Content-Type' => 'application/json'],
    'json' => [
                "client_id" => "xxx",
                "secret" => "xxx",
                "access_token" => [ "access-sandbox-xxx" ] ,
                "days_requested" => 30,
                "options" => [
                        "client_report_id" => "ENTER_CLIENT_REPORT_ID_HERE",
                        "webhook" => "https://www.example.com/webhook",
                        "user" => [
                                "client_user_id" => "ENTER_USER_ID_HERE",
                                "first_name" => "ENTER_FIRST_NAME_HERE",
                                "middle_name" => "ENTER_MIDDLE_NAME_HERE",
                                "last_name" => "ENTER_LAST_NAME_HERE",
                                "ssn" => "111-22-1234",
                                "phone_number" => "1-415-867-5309",
                                "email" => "ENTER_EMAIL_HERE"
                            ]
                    ]
                ]   
]); 

display error

Client error: POST https://sandbox.plaid.com/asset_report/create resulted in a 400 Bad Request response: { "display_message": null, "documentation_url": "https://plaid.com/docs/?ref=error#invalid-request-errors", "error (truncated...)

What is missing on my guzzle code.

please help.

CodePudding user response:

Try using body key instead of json

CodePudding user response:

Laravel has the Http client available which is a wrapper around guzzle:

$response = Http::withToken($token)->post('yourUrl', [ params ]);

dd($response->body());

CodePudding user response:

You should try replacing json with body:

Also, convert the array into valid JSON format.

References: https://stackoverflow.com/a/39525059/5192105

use GuzzleHttp\Client;

$guzzle = new Client;
$getAccestToken = $guzzle->request('POST', 
'https://sandbox.plaid.com/asset_report/create', [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode([
            "client_id" => "xxx",
            "secret" => "xxx",
            "access_token" => [ "access-sandbox-xxx" ] ,
            "days_requested" => 30,
            "options" => [
                    "client_report_id" => "ENTER_CLIENT_REPORT_ID_HERE",
                    "webhook" => "https://www.example.com/webhook",
                    "user" => [
                            "client_user_id" => "ENTER_USER_ID_HERE",
                            "first_name" => "ENTER_FIRST_NAME_HERE",
                            "middle_name" => "ENTER_MIDDLE_NAME_HERE",
                            "last_name" => "ENTER_LAST_NAME_HERE",
                            "ssn" => "111-22-1234",
                            "phone_number" => "1-415-867-5309",
                            "email" => "ENTER_EMAIL_HERE"
                        ]
                ]
            ])   
]); 
  • Related