我正在尝试使用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');
答案 0 :(得分:0)
Your error may be from api server. If your api also written using laravel, i think this is CSRF token mismatch exception error.