[英]Client Error 404 not found when sending a post request with guzzle
我想使用 guzzle 向外部 api 發送 post 請求,但出現此錯誤:
客戶端錯誤: POST https://api.platform.ly/
導致404 Not Found
響應:
{"status":"error","message":"Missing Parameters"}
$client = new \GuzzleHttp\Client();
$url = "https://api.platform.ly/";
$body['api_key'] = ENV('PLATFORMLY_KEY');
$body['action'] = 'add_contact';
$body['value'] = [
'project_id' => '1589',
'email' => $user->email,
'name' => $user->name
];
$request = $client->post($url, ['form_params'=>$body]);
dd($request);
$response = $request->send();
dd($response);
這應該可以解決您的問題。 每個平台文檔的value
字段都需要一個 JSON 字符串,因此請像這樣使用json_encode
。
$body['value'] = json_encode([
'project_id' => '1589',
'email' => $user->email,
'name' => $user->name
]);
我遇到了同樣的問題,花了一分鍾才弄明白,因為不清楚。
這是實現了 json_encode 的代碼片段。
$client = new \GuzzleHttp\Client();
$url = "https://api.platform.ly/";
$body['api_key'] = ENV('PLATFORMLY_KEY');
$body['action'] = 'add_contact';
$body['value'] = json_encode([
'project_id' => '1589',
'email' => $user->email,
'name' => $user->name
]);
$request = $client->post($url, ['form_params'=>$body]);
dd($request);
$response = $request->send();
dd($response);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.