Guzzle3发送原始发布请求

问题描述 投票:1回答:1

我正在使用Guzzle3(v3.9.3)的项目中工作,我想知道如何发送原始的发帖请求,我已经尝试过这些解决方案,但是没有一个对我有用。我正在使用这个Guzzle3 manual

解决方案:

$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' =>  'Bearer '.$token,
'Accept' => 'application/json'
));

$body = '{"filter_":{"user":{"email":"[email protected]"}}}';
$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":"[email protected]"}}}';

$req = $client->post($url, array(), array(),
 array(
    'cert' => array($certification, 'password'),
 )
);

$req->setBody($body);
$response = json_decode($client->send($req)->getBody(true)); 

他们都没有为我工作,

错误:客户端错误响应[状态代码] 404 [原因短语]未找到[URL]

我尝试了一些在Internet上找到的解决方案(但对于Guzzle6),它可以工作,但是我没有得到正确的结果(它没有考虑我发送的过滤器,即邮件地址,所以我获得所有结果)

...
$body = array(
'filter_' => array(
   'user' => array( "email" => $email )
 )
);

$req = $client->post($url, array(),array('body'=> $body),
array(
    'cert' => array($certification, 'password'),
)
);
...

在邮递员上,WS工作的电话。

提前感谢

php guzzle
1个回答
0
投票

如果有人需要,我会发布响应,我必须将所有块放在try catch之间

try{
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' =>  'Bearer '.$token,
));
$body = '{"filter_":{"user":{"email":"[email protected]"}}}';
$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);
}
© www.soinside.com 2019 - 2024. All rights reserved.