我的php代码中关于curl和send json with post方法的错误

我的php代码中关于curl和send json with post方法的错误,php,json,curl,Php,Json,Curl,我的php代码中关于curl和send json with post方法的错误 这是我的send.php代码: $url = "http://localhost/get.php"; $data = array( 'item1' => 'value1', 'item2' => 'value2', 'item3' => 'value3' ); $content = json_encode($data); $cur

我的php代码中关于curl和send json with post方法的错误 这是我的send.php代码:

$url = "http://localhost/get.php";    
$data = array(
    'item1'      => 'value1',
    'item2'      => 'value2',
    'item3'       => 'value3'
);


$content = json_encode($data);

$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);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

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


curl_close($curl);

$response = json_decode($json_response, true);
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $data = json_decode(file_get_contents("php://input"));
  print_r($data);
}
下面是我的get.php代码:

$url = "http://localhost/get.php";    
$data = array(
    'item1'      => 'value1',
    'item2'      => 'value2',
    'item3'       => 'value3'
);


$content = json_encode($data);

$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);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

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


curl_close($curl);

$response = json_decode($json_response, true);
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $data = json_decode(file_get_contents("php://input"));
  print_r($data);
}
然后向我显示此错误:

Error: call to URL http://localhost/get.php failed with status 200, response stdClass Object ( [item1] => value1 [item2] => value2 [item3] => value3 ) , curl_error , curl_errno 0

伙计们该怎么办?

你们期待什么?您从您点击的url得到了http 200响应,而您的代码只需要201。我们帮不了你。您就是用php代码生成
200OK
的人。200是一个有效的响应,它表示ok@MarcB我想用post发送json并第一次显示它training@aynber是的,200是可以的,但是它返回201。好吧,您的代码无论如何都不会产生那个错误。curl_exec要么返回字符串,要么返回布尔值false。它不会响应stdclass对象,PHP也不能进行时间旅行。稍后将对json进行解码,但在抛出该错误时,仍然只有文本。