php - Guzzle3 发送原始的 Post 请求
问题描述
我正在开发使用 Guzzle3 (v3.9.3) 的项目,我想知道如何发送原始发布请求,我已经尝试过这些解决方案,但没有一个对我有用。我正在使用这个 Guzzle3手册。
解决方案 :
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json'
));
$body = '{"filter_":{"user":{"email":"aaa@test.com"}}}';
$req = $client->post($url, array(), $body,
array(
'cert' => array($certification, 'password'),
)
);
$response = json_decode($client->send($req)->getBody(true));
解决方案 2:
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json'
));
$body = '{"filter_":{"user":{"email":"aaa@test.com"}}}';
$req = $client->post($url, array(), array(),
array(
'cert' => array($certification, 'password'),
)
);
$req->setBody($body);
$response = json_decode($client->send($req)->getBody(true));
他们都没有为我工作,
错误:客户端错误响应 [状态代码] 404 [原因短语] 未找到 [url]
我尝试了一些在互联网上找到的解决方案(但对于 Guzzle6)它可以工作,但我没有得到正确的结果(它没有考虑我发送的过滤器,即邮件地址,所以我得到了所有结果)
...
$body = array(
'filter_' => array(
'user' => array( "email" => $email )
)
);
$req = $client->post($url, array(),array('body'=> $body),
array(
'cert' => array($certification, 'password'),
)
);
...
在邮递员上,对 WS 的调用起作用了。
提前致谢
解决方案
如果有人需要,我会发布回复,我必须将所有的块放在 try catch 之间
try{
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
));
$body = '{"filter_":{"user":{"email":"aaa@test.com"}}}';
$req = $client->post($url, array(), $body,
array(
'cert' => array($certification, 'password'),
)
);
$response = json_decode($client->send($req)->getBody(true));
catch(Guzzle\Http\Exception\BadResponseException $e){
$response = json_decode($e->getResponse()->getBody(),true);
}
推荐阅读
- docker - 容器化单体的可扩展性是否不如微服务?
- javascript - 如何使用 VueJS 实现 Gmail API
- snowflake-cloud-data-platform - Snowflake:以多种分隔符格式导出数据
- ruby - Ruby 版本失败 2.7.0:错误:Permission denied @ apply2files - /usr/local/share/Library/
- amazon-ec2 - 断开和重新连接 nvme
- json - 从 SwiftUI 中的 URL 获取 JSON
- python - 多个 groupby 并获得唯一计数
- docker - python Mysql.connector 不连接但 mysql 服务器已启动并正在工作
- javascript - CSS 中的悬停和 JavaScript 中的可见性是否不兼容?
- go - 向项目添加 gomock 测试会在运行测试时提供“构建约束排除所有 Go 文件”