简体   繁体   English

访问从PHP cURL发送的JSON POST数据

[英]access json POST data sent from PHP cURL

I'm trying to access POST data sent from this cURL request. 我正在尝试访问从此cURL请求发送的POST数据。

 $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. 在data.php中,我想访问从cURL发送的JSON,并在基于它们的验证之后返回数据。 Please help me access json post data from data.php . 请帮助我从data.php访问json post数据。 So far, data.php only returns an empty post array. 到目前为止, data.php仅返回一个空的post数组。

print_r($_POST);

Use this php://input 使用这个php://input

example

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM