我的PHP代码中有关curl的错误,并使用post方法发送json

时间:2016-09-09 16:18:20

标签: php json curl

我的php代码中有关curl的错误,并使用post方法发送json 这是我的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);

这是我的get.php代码:

    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

怎么做男人?

0 个答案:

没有答案