首页 > 解决方案 > php curl PUT 请求

问题描述

我正在尝试使用 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;

如何解决这个问题?

标签: php-curl

解决方案


与您一样Content-Typeapplication/json但您正在以 url 编码模式发送帖子数据,这可能会触发错误。更改此行

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

喜欢下面

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

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


推荐阅读