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.
<em>//set your json url</em>
$url = "https://jsonplaceholder.typicode.com/todos/1";

<em>//call the api url</em>
$json = file_get_contents($url);
$json = json_decode($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