PHP Get, Write, Read, Load, JSON Data from external URL

PHP JSON Data from external URL; Through this tutorial. i am going to show you how to Get, Load, Read, Save/Write, JSON File from Url or apis in PHP.

PHP Get, Write, Read, Load, JSON Data from external URL

Let’s use the following examples with php code to get, read, write and load json data from url or apis in php; as follows:

  • PHP read JSON file From URL
  • PHP get json data from external url
  • PHP Write or save JSON to a JSON file or Text File

PHP read JSON file From URL

Using the PHP file_get_contents() function, you can get or read or load file from the given path (Url or external url).

Let’s see the following example to read json data from external url in PHP; as follows:

<?php
$url = 'your json file path';
//read json file from url in php
$readJSONFile = file_get_contents($url);
print_r($readJSONFile); // display contents
?>

PHP get json data from external url

Using the the file_get_contents() function and json_decode() function of PHP, you can get json data from external url.

  • File get_get_conents() get that is used to get or read file from the given path (Url or Source).
  • json_decode() function of PHP, which is used to convert JSON file contents to PHP Array

Let’s see the following example to get json data from external url; as follows:

<?php
$url = 'your json file path';
//read json file from url in php
$readJSONFile = file_get_contents($url);
//convert json to array in php
$array = json_decode($readJSONFile, TRUE);
var_dump($array); // print array
?>

PHP Write or save JSON to a JSON file or Text File

Using the file_put_contents() function of PHP, you can write or save data from a given path of the JSON file or text file.

Let’s see the following example to write or save JSON to a JSON file or text file in php; as follows:

<?php
$path = 'Url file path';
$data = 'Hello world';
file_put_contents($path, $data)
?>

Recommended PHP Tutorials

Leave a Comment