我有一个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>";
答案 0 :(得分:0)
答案 1 :(得分:0)
您的POST不包含键/值对,因此没有任何内容可以填充$ _POST。您可以尝试将其格式化为键/值对,或者只需阅读php://input
中的test.php
:
<pre><?php var_dump(file_get_contents('php://input'));