Send Receive JSON POST data PHP - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, March 27, 2022

Send Receive JSON POST data PHP

I have a device where I execute this php file


$url = "https://xxxxx.com/raspberry/json.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
{
  "Device": "102",
  "Status": "Power on",
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

The receiver file

$data = file_get_contents('php://input');

echo $data;

$json = json_decode($data, true);

echo "Device: " . $json['Device'];
echo "Device: " . $json->Device;

print me

string(62) "{
  "Device": "125",
  "Status": "Power on",
}Device: Device: "

so why don't print me the single data? I expect it prints also the single element. thanks



source https://stackoverflow.com/questions/71630877/send-receive-json-post-data-php

No comments:

Post a Comment