Dilasha Dilasha - 4 months ago 16
PHP Question

access json POST data sent from PHP cURL

I'm trying to access POST data sent from this cURL request.

$param = array(
'foo' => 'bar',
'bar' => 'foo'
)
$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => 'http://localhost/mysite/data.php',
CURLOPT_POST => 'TRUE',
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: '.strlen(json_encode($param))
),
CURLOPT_POSTFIELDS => json_encode($param),
)
);

$result = curl_exec($ch);
curl_close($ch);
print_r($result);


In data.php I want to access the JSON sent from cURL and return data after some validations based on them. Please help me access json post data from
data.php
. So far,
data.php
only returns an empty post array.

print_r($_POST);

Answer

Use this php://input

example

echo json_decode(file_get_contents('php://input'));
Comments