In this tutorial, I will give you an example of “How to decode JSON response in curl request in Laravel,” so you can easily apply it to your Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 applications.
Some APIs are decoded easily, and some APIs are highly secure and support the HTTPS protocol, but it’s not easy to decode JSON and get data and other tokens with an API using curl.
Json decode response in curl request in Laravel
Decoding JSON Response from cURL, We will fix and access a highly secured API; we will use some lines of code to access it in the Laravel application.
$c = curl_init();
$url = "https://apiv2.shiprocket.in/v1/external/auth/login";
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_POSTFIELDS, "email=email@gmail.com&password=email@gmail.com");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$server_output = curl_exec($c);
curl_close($c);
$server_output = json_decode($server_output,true);
//echo "<pre>"; print_r($server_output); die;
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
I hope that this article helped you learn how to fix the JSON decode issue in a curl request in the Laravel application. You may also want to check out our guide on How to Search for first name and last name in Join Query in Laravel.