PHP的卷曲PUT请求

时间:2020-10-23 18:48:57

标签: php-curl

我正在尝试使用curl php发送一个PUT请求,但是我得到了"status":103,"message":"Invalid JSON Request"作为响应。

$data = array('request'=>'personalized-accounts',
'name'=>'john doe',
'reference'=>'162329305158',
'email'=>'ade@gmail.com');
$url = 'https://demo.api.gladepay.com/resources';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Accept-Encoding: gzip, deflate, br',
  'MID: GP0000001',
  'KEY: 123456789',
  'Content-Type:application/json'

));
$response = curl_exec($ch);

echo $response;

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于您的Content-Typeapplication/json,但是您以url编码模式发送帖子数据,这可能会触发错误。 更改此行

curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

喜欢下面

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));

用于在发布请求中发送json数据。

相关问题