Rathik's dev blog

How to Get Json Data From url in Laravel

A laptop computer sitting on top of a wooden desk
Published on
/1 mins read/---
how-to-get-json-data-from-url-in-laravel.jpg

I'll demonstrate how to retrieve JSON from a URL in a php script in this tutorial. JSON has gained popularity as a format for data sharing and web service outputs. PHP makes it pretty easy to perform an HTTP request and parse a JSON response from a URL, however beginners could find learning how to parse json challenging.

i will use an sample json url to retrive data by using this code.

  1. Its the script that you can copy and return the data. You can directly copy this code and enter your function & run that.
//set your json url
$url = "https://jsonplaceholder.typicode.com/todos/1";
 
//call the api url
$json = file_get_contents($url);
$json = json_decode($json);
dd($json);
 
  1. Now you will get the results like this & you can use this where you need.
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Read my others post

Dont forget to comment if need any help