【问题标题】:Guzzle3 send raw Post requestGuzzle3 发送原始的 Post 请求
【发布时间】:2019-10-02 23:00:59
【问题描述】:

我正在开发使用 Guzzle3 (v3.9.3) 的项目,我想知道如何发送原始发布请求,我尝试了这些解决方案,但没有一个对我有用。我正在使用这个 Guzzle3 manual

解决方案:

$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 工作。

提前致谢

【问题讨论】:

  • 你被限制在 Guzzle3 有什么原因吗?您能否提供有关如何配置 Postman 请求的更多信息? (如果可以在 Postman 中完成,则可以在 Guzzle 中完成。)

标签: php guzzle


【解决方案1】:

如果有人需要,我会发布回复,我必须把所有的块放在 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);
}

【讨论】:

  • 你说捕获异常并将其解码为正确的数据是一种解决方案?
  • 当 WS 返回空响应时,我收到我提到的错误,当我添加带有 BadResponseException 的 bloc catch 并调试 $e 时,我看到我得到空响应!
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 2016-02-11
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多