【问题标题】:Getting ' 400 Bad Request ' response using Guzzle in laravel在 laravel 中使用 Guzzle 获得“400 Bad Request”响应
【发布时间】:2017-12-17 06:29:18
【问题描述】:

我正在尝试使用 Guzzle 在我的 laravel 应用程序中使用第三方 api,但我收到 400 Bad Request 错误。以下是控制器代码

<?php
public function sendEmailAmazon(Request $request)
{
    $data = json_decode(json_encode($request->all()));
    $mailaddresses = $data->tomail;

    // dd($mailaddresses);

    foreach ($mailaddresses as $mailaddress) {
        $client = new Client();
        $value = Config::get('app');
        $maildata = array(
            'mailId' => null,
            'type' => '1',
            'from' => array(
                'name' => $value['from_name'],
                'address' => $value['from_email'],
                'mobile' => $value['from_mobile'],
            ),
            'to' => $data->tomail,
            'cc' => null,
            'replyTo' => null,
            'subject' => $data->subject,
            'body' => $data->msg,
            'text' => null,
            'attachment' => null,
            'personalization' => array(
                'salutation' => 'Dear',
                'useFirstNameOnly' => true
            ),
            'apiKey' => $value['api_key'],
            'apiSecret' => $value['api_secret'],
            'customerId' => $value['customer_id'],
        );
        $data2 = json_encode($maildata);
        $url = $value['api_url'];
        try {
            $res = $client->post($url, ['Accept' => 'application/json', 'json' => $data2, ]);
            return $res;
            $this->addEmailHistory($data);
            return response()->json(['success' => 'true', 'data' => $data2, 'message' => 'Email sent']);
        } catch(Exception $ex) {
            return Common::getJsonResponse(false, $ex->getMessage() , 300);
        }
    }
}

我也尝试了documentation 中所述的以下代码,但仍然出现相同的错误:

$r = $client->request('POST', $url, [
    'json' => $data2
]); 

我的路线

Route::post('email/sendEmailAmazon', 'EmailsController@sendEmailAmazon');

【问题讨论】:

    标签: php laravel laravel-5 guzzle


    【解决方案1】:

    您的错误可能来自 api 服务器。如果您的 api 也是使用 laravel 编写的,我认为这是 CSRF 令牌不匹配异常错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2020-03-19
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多