您现在的位置是:首页 > Javascript
当前栏目
guzzle 使用 json 作为主体请求接口
2023-02-18 16:47:46 时间
在使用第三方插件 Guzzle 请求微信素材管理接口:
接口说明
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
#参数
{
"type":TYPE,
"offset":OFFSET,
"count":COUNT
}
错误请求代码
$client = new Client();
$response = $client->post("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=$token", [
'form_params' => [
"type"=>"news",
"offset"=>0,
"count"=>10
]
]);
错误原因:请求体格式必须是json 格式 尝试修改代码如下:
$client = new Client();
$response = $client->post("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=$token", [
'form_params' =>json_encode([
"type"=>"news",
"offset"=>0,
"count"=>10
])
]);
仍旧报错。
经查看Guzzle 手册,正确代码如下:
$client = new Client();
$response = $client->post("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=$token", [
'json' => [
"type"=>"news",
"offset"=>0,
"count"=>10
]
]);
可以正常获取到数据。
相关文章
- [javaEE] jsp的九大隐式对象
- [javaEE] jsp的指令
- [javaEE] jsp入门
- [javascript] Promise简单学习使用
- [android] WebView与Js交互
- [javascript] 看知乎学习js事件触发过程
- [javascript] 看知乎学习js闭包
- [angularjs] angularjs系列笔记(八)事件
- [angularjs] 前端路由实现单页跳转
- [angularjs] angularjs系列笔记(七)HTML DOM
- JS对象拷贝:深拷贝和浅拷贝
- [angularjs] angularjs系列笔记(六)http
- [angularjs] angularjs系列笔记(五)Service
- [angularjs] angularjs系列笔记(四)过滤器
- [angularjs] angularjs系列笔记(四)控制器
- [angularjs] angularjs系列笔记(三)模型
- [angularjs] angularjs系列笔记(二)指令
- [nodejs] nodejs开发个人博客(七)后台登陆
- [angularjs] angularjs系列笔记(一)简介
- [nodejs] nodejs开发个人博客(六)数据分页