Guzzle3 发送原始 Post 请求
Guzzle3 send raw Post request
我正在开发使用 Guzzle3 (v3.9.3) 的项目,我想知道如何发送原始 post 请求,我已经尝试过这些解决方案,但其中 none 有效为了我。我正在使用这个 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));
None 对我有用,
错误:客户端错误响应 [状态代码] 404 [原因短语] 未找到 [url]
我尝试了一些在互联网上找到的解决方案(但对于 Guzzle6)它有效但我没有得到正确的结果(它没有考虑我发送的过滤器,即邮件地址, 所以我得到了所有结果)
...
$body = array(
'filter_' => array(
'user' => array( "email" => $email )
)
);
$req = $client->post($url, array(),array('body'=> $body),
array(
'cert' => array($certification, 'password'),
)
);
...
在 postman 上调用 WS 工作。
提前致谢
我发布了响应以防有人需要,我不得不将所有 bloc 放在 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);
}
我正在开发使用 Guzzle3 (v3.9.3) 的项目,我想知道如何发送原始 post 请求,我已经尝试过这些解决方案,但其中 none 有效为了我。我正在使用这个 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));
None 对我有用,
错误:客户端错误响应 [状态代码] 404 [原因短语] 未找到 [url]
我尝试了一些在互联网上找到的解决方案(但对于 Guzzle6)它有效但我没有得到正确的结果(它没有考虑我发送的过滤器,即邮件地址, 所以我得到了所有结果)
...
$body = array(
'filter_' => array(
'user' => array( "email" => $email )
)
);
$req = $client->post($url, array(),array('body'=> $body),
array(
'cert' => array($certification, 'password'),
)
);
...
在 postman 上调用 WS 工作。
提前致谢
我发布了响应以防有人需要,我不得不将所有 bloc 放在 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);
}