Answers for "php get json response"

PHP
2

php return json

// function to return JSON response, you can set cors, also give status & data via arguments
function json($status, $data) {
        $cors = "*";
        header("Access-Control-Allow-Origin: $cors");
        header('Content-Type: application/json; charset=utf-8');
        http_response_code($status);
        echo json_encode($data);
    }

// example
json(200, [
	"status" => 200,
  	"message" => "action completed successfully",
  	"id" => 44236
]);
Posted by: Guest on October-04-2021
5

php return json

header('Content-type: application/json');
echo json_encode($array);
Posted by: Guest on March-20-2020
2

php return json data

header('Content-Type: application/json'); 

$colors = array("red","blue","green");
echo json_encode($colors);
Posted by: Guest on October-30-2019
0

php json request get value

<?php
$jsonurl = "https://reqres.in/api/users/2";
$json = file_get_contents($jsonurl);
$jsonDecode = json_decode($json, true);
echo $jsonDecode['data']['email'];
?>
Posted by: Guest on October-27-2020
0

php get json array

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

$yummy = json_decode($json, true);

echo $yummy['toppings'][2]['type']; //Maple
Posted by: Guest on February-01-2021

Browse Popular Code Answers by Language