使用 JSON 发送数据时,cURL php POST 错误 header
cURL php POST error header when send data with JSON
我正在尝试 post 带有 JSON 数据的 cURL 数据,我已经更改了 header 但我遇到了错误,这是我的代码
public static function curlValidation($url, $params) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, array("Accept:application/json", "Content-Type:application/json"));
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
if($result === false)
return curl_error($ch);
curl_close($ch);
return $result;
}
$params 值为 JSON
{
"NIK":"123",
"NAMA":"ASD",
"TGL_LHR":"123",
}
出现这样的错误
'HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: origin, x-requested-with, content-type
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 20 Sep 2021 02:07:58 GMT
Set-Cookie: SRVNAME=app05-nutanix; path=/
{\"msg\":\"Content type \'application/x-www-form-urlencoded;charset=UTF-8\' not supported\",\"error\":false}'
尝试在您的函数中执行此操作 - 我认为您的 headers 数据已损坏:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
根据您收到的消息,您发送的内容类型有误。
将您函数中的 curl 行替换为这些行,或者仅将您的 $headers 行替换为本示例中的这 3 行 $headers 行。
这不起作用的原因是你在这行有错误:
curl_setopt($ch,CURLOPT_HEADER, array("Accept:application/json", "Content-Type:application/json"));
您应该使用的常数是 CURLOPT_HTTPHEADER
我正在尝试 post 带有 JSON 数据的 cURL 数据,我已经更改了 header 但我遇到了错误,这是我的代码
public static function curlValidation($url, $params) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, array("Accept:application/json", "Content-Type:application/json"));
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
if($result === false)
return curl_error($ch);
curl_close($ch);
return $result;
}
$params 值为 JSON
{
"NIK":"123",
"NAMA":"ASD",
"TGL_LHR":"123",
}
出现这样的错误
'HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: origin, x-requested-with, content-type
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 20 Sep 2021 02:07:58 GMT
Set-Cookie: SRVNAME=app05-nutanix; path=/
{\"msg\":\"Content type \'application/x-www-form-urlencoded;charset=UTF-8\' not supported\",\"error\":false}'
尝试在您的函数中执行此操作 - 我认为您的 headers 数据已损坏:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
根据您收到的消息,您发送的内容类型有误。
将您函数中的 curl 行替换为这些行,或者仅将您的 $headers 行替换为本示例中的这 3 行 $headers 行。
这不起作用的原因是你在这行有错误:
curl_setopt($ch,CURLOPT_HEADER, array("Accept:application/json", "Content-Type:application/json"));
您应该使用的常数是 CURLOPT_HTTPHEADER