Php GuzzleHttp\Client 400 Laravel 5.5上的错误请求

Php GuzzleHttp\Client 400 Laravel 5.5上的错误请求,php,laravel,curl,guzzle,Php,Laravel,Curl,Guzzle,我向API发送POST请求,Curl返回200和正确的响应。 在使用GuzzleHttp\Client实现时,我收到一个400错误的请求,这说明我的格式有什么问题 以下是我使用Laravel返回400个错误请求的代码: $client = new Client(); $URI = 'http://api.example.com'; $params['headers'] = ['Content-Type' => 'application/json', 'apikey' =>

我向API发送POST请求,Curl返回200和正确的响应。 在使用GuzzleHttp\Client实现时,我收到一个400错误的请求,这说明我的格式有什么问题

以下是我使用Laravel返回400个错误请求的代码:

 $client = new Client();

$URI = 'http://api.example.com';
$params['headers'] = ['Content-Type' => 'application/json',
    'apikey' => config('app._api_key'),
    'debug' => true
];
$params['form_params'] = [
    'sender' => 'Test_sender',
    'recipient' => config('app.test_recipient'),
    'message_body' => 'Test  body'
];
return $response = $client->post($URI, $params);
Curl(返回200):


这就是正确的错误处理:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

try {

    $response = $client->get(YOUR_URL, [
        'connect_timeout' => 10
    ]);

    // Here the code for successful request

} catch (RequestException $e) {

    // Catch all 4XX errors 

    // To catch exactly error 400 use 
    if ($e->getResponse()->getStatusCode() == '400') {
            echo "Got response 400";
    }

    // You can check for whatever error status code you need 

} catch (\Exception $e) {

    // There was another exception.

}
实现:

请尝试以下代码:

$client = new \GuzzleHttp\Client(['headers' => ['Content-Type' => 'application/json',
                                                'apikey'=> config('app._api_key'),
                                                'debug' => true
                                                ]
                                ]);
$URI = 'http://api.example.com';
$body['sender']='Test_sender';
$body['recipient']=config('app.test_recipient');
$body['message_body']='Test  body';
$body=json_encode($body);
$URI_Response = $client->request('POST',$URI,['body'=>$body]);
$URI_Response =json_decode($URI_Response->getBody(), true);
return $URI_Response;

注意:我建议您处理错误。请参阅

您可以这样处理错误

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Exception;


try{
    $client = new Client();

    $response = $client->request('POST', $url,[
         'headers' => $header,
         'form_params' => $form-params
    ]);
    $body = $response->getBody();
    $status = 'true';
    $message = 'Data found!';
    $data = json_decode($body);
}catch(ClientException $ce){
    $status = 'false';
    $message = $ce->getMessage();
    $data = [];
}catch(RequestException $re){
   $status = 'false';
   $message = $re->getMessage();
   $data = [];
}catch(Exception $e){
   $this->status = 'false';
   $this->message = $e->getMessage();
   $data = [];
}

return ['status'=>$status,'message'=>$message,'data'=>$data];

好的,在我实现之前,我如何得到具体的错误?echo 400没有帮助。echo$e->getResponse()->getStatusCode()显然我在Laravel 5.4上,我在nullok上得到了对成员函数getStatusCode()的调用。环境变量不在适当的位置,现在刚得到400,没有详细信息。SA 400表示请求格式不正确。换句话说,客户端发送到服务器的数据流不符合规则。您是否尝试将“form_params”更改为“json”?您的头声明请求是JSON请求,但您正在发送未编码的数据
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Exception;


try{
    $client = new Client();

    $response = $client->request('POST', $url,[
         'headers' => $header,
         'form_params' => $form-params
    ]);
    $body = $response->getBody();
    $status = 'true';
    $message = 'Data found!';
    $data = json_decode($body);
}catch(ClientException $ce){
    $status = 'false';
    $message = $ce->getMessage();
    $data = [];
}catch(RequestException $re){
   $status = 'false';
   $message = $re->getMessage();
   $data = [];
}catch(Exception $e){
   $this->status = 'false';
   $this->message = $e->getMessage();
   $data = [];
}

return ['status'=>$status,'message'=>$message,'data'=>$data];