php-curl - 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;
如何解决这个问题?
解决方案
与您一样Content-Type
,application/json
但您正在以 url 编码模式发送帖子数据,这可能会触发错误。更改此行
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
喜欢下面
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
用于在 post 请求中发送 json 数据。
推荐阅读
- python - 使用 Waitress 和 Flask 运行两次 API 调用
- r - 如何在ggplot中绘制表格对象
- javascript - 打开图片时如何读取图片的尺寸?
- python - 如何根据其他列的平均值创建多列
- curl - 无法使用 GitHub API v3 为组织存储库创建问题
- php - 在 xampp 3.2.2 和 php 7.2.10 中安装作曲家有问题
- reactjs - Babel 插件类属性 – React 箭头函数
- android-studio - 无法在 android studio 中使用我的小米米 A1
- sql - 从sql plus中的字符串中删除第一个字符
- python-3.x - 尝试获取字典以将项目添加为列表被迭代,但不起作用。有没有办法来解决这个问题?