how to get json data in php from url?

Today, We want to share with you how to get json data in php from url.In this post we will show you how to print json data in php, hear for how to fetch data from api in php we will give you demo and example for implement.In this post, we will learn about Receive JSON POST Data Using PHP with an example.

how to get data from json array in php?

JSON Handling with PHP: How to Encode, Write, Parse, Decode and Convert Examples.

get json from url php

$url = "http://www.welcomepakainfo.com";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo "My Light Live token: ". $json_data["access_token"];

fetch value from json link in php

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'url_here');
$response = curl_exec($ch);
curl_close($ch);

$obj = json_decode($response);
echo $obj->access_token;

Example of a JSON Object and Array

{
    "ABC_Internamtional_sport_Champions" : {
        "2022" : "India",
        "2014" : "Lanka",
        "2010" : "Surat",
        "2006" : "Rajkot",
        "2002" : "Ahemdabad"
     }
}

Example of a JSON Array

{
    "ABC_Last_Five_Champions" : ["India","Lanka","Surat","Rajkot","Ahemdabad"]
}

JSON Handling with PHP by Parsing File Data

{
	"FINAL_submitted_all_brand_brands":
	[ 
		{
			"Year": "2022",
			"data": 
			{
				"Champion": "India",
				"Team-run": "4-2",
				"Backer": "Pakistan"
			}
		},
	    {
			"Year": "2014",
			"data": 
			{
				"Champion": "Lanka",
				"Team-run": "1-0",
				"Backer": "Kochi"
			}
		},
	    {
			"Year": "2010",
			"data": 
			{
				"Champion": "Surat",
				"Team-run": "1-0",
				"Backer": "SalPipaliya"
			}
		}
	]
}

use of the power of PHP built-in functions.

<?php 
$JSON = file_get_contents("input.json");
$response_json_data = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($JSON, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($response_json_data as $key => $val) {
    if(!is_array($val)) {
        if($key == "Year") {
            print "<br/>";
        }
    print $key."    : ".$val . "<br/>";
    }
}
?>

Free Live Chat for Any Issue

How to Decode JSON to Array

json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] ) : mixed

<?php 
$response_json_data = '{
    "ABC_Last_Internamtional_sport_final":
    {
        "Year": "2022",
        "data":
        {
            "Champion": "India",
            "Team-run": "4-2",
            "Backer": "Pakistan"
        }
    }
}';

$response_json_data = json_decode($response_json_data, TRUE);

print "<PRE>";
print_r($response_json_data);
?>

Decode JSON to Object

<?php 
$response_json_data = '{
    "ABC_Last_Internamtional_sport_final":
    {
        "Year": "2022",
        "data":
        {
            "Champion": "India",
            "Team-run": "4-2",
            "Backer": "Pakistan"
        }
    }
}';

$outputObject = json_decode($response_json_data);

print "<PRE>";
print_r($outputObject);
?>

Convert PHP Array to JSON

<?php 
$response_json_data = array (
            "ABC_Last_Internamtional_sport_final" => array (
                    "Year" => "2022",
                    "data" => array ( "Champion" => "India", "Team-run" => "4-2", "Backer" => "Pakistan")
                )
            );

$encodedJSON = json_encode($response_json_data, JSON_PRETTY_PRINT);

print($encodedJSON);
?>

How to Access a JSON Feed and Display Data

<?php 
$response_json_data = "\path\input.json";
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $response_json_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);

curl_close($ch);
$outputArray = json_decode($response);

print "<PRE>";
print_r($outputArray);
exit;
?>

How to Convert JSON to JavaScript Object

<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
	var JSONInputData = '{ "FINAL_submitted_all_brand_brands": [ { "Year": "2022", "data": { "Champion": "India", "Team-run": "4-2", "Backer": "Pakistan" } }, { "Year": "2014", "data": { "Champion": "Lanka", "Team-run": "1-0", "Backer": "Kochi" } } ] }';
	var LastJSObjData = $.parseJSON(JSONInputData);
	$("#is_active").html("View JavaScript data object structure in the browser console");
	console.log(LastJSObjData);
});
</script>
</head>
<body>
    <div id="is_active"></div>
</body>
</html>

Loop through the JSON object using Javascript

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
	var JSONInputData = '{ "FINAL_submitted_all_brand_brands": [ { "Year": "2022", "data": { "Champion": "India", "Team-run": "4-2", "Backer": "Pakistan" } }, { "Year": "2014", "data": { "Champion": "Lanka", "Team-run": "1-0", "Backer": "Kochi" } } ] }';
	var LastJSObjData = $.parseJSON(JSONInputData);
    for (var key in LastJSObjData["FINAL_submitted_all_brand_brands"]) {
            
          document.write("<br/><u>" + LastJSObjData["FINAL_submitted_all_brand_brands"][key]["Year"]+"</u><br/>");
          for (var k in LastJSObjData["FINAL_submitted_all_brand_brands"][key]["data"]) {
        	      document.write(k + ": " + LastJSObjData["FINAL_submitted_all_brand_brands"][key]["data"][k]+"<br>");
          }
    }
});
</script>

Convert JavaScript Object to JSON

<script>
var objectArray = {"FINAL_submitted_all_brand_brands": [ 
        { 
            "Year": "2022", 
            "data": 
                { 
                    "Champion": "India", 
                    "Team-run": "4-2", 
                    "Backer": "Pakistan" 
                } 
        }, 
        { 
            "Year": "2014", 
            "data": 
                { 
                    "Champion": "Lanka", 
                    "Team-run": "1-0", 
                    "Backer": "Kochi" 
                } 
        } 
]};
var JSONStringData = JSON.stringify(objectArray);
document.write(JSONStringData);
</script>

I hope you get an idea about php parse json from url.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.

Rate this post