如何获取通过curl发送到服务器的json数据

时间:2014-10-02 20:19:46

标签: php curl

我有一个curl脚本将json数据发布到test.php但是在test.php中我无法打印发送的数据。我不知道它是否正常工作。如何检索发送的数据?

cUrl脚本:

$url = "test.php";    
$content = json_encode(array('asdf'=>1));

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

echo $json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

echo" curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl);
curl_close($curl);

我无法在

中输出任何内容

test.php的:

    echo "<pre>";
    print_r($_REQUEST);
    print_r($_POST);
    echo "</pre>";

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您的POST不包含键/值对,因此没有任何内容可以填充$ _POST。您可以尝试将其格式化为键/值对,或者只需阅读php://input中的test.php

<pre><?php var_dump(file_get_contents('php://input'));