[英]Client Error 404 not found when sending a post request with guzzle
I want to send a post request to an external api using guzzle , but i get this error :我想使用 guzzle 向外部 api 发送 post 请求,但出现此错误:
Client error: POST https://api.platform.ly/
resulted in a 404 Not Found
response:客户端错误:
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);
This should fix your problem.这应该可以解决您的问题。 A JSON string is needed on the
value
field per platformly docs, so utilize json_encode
like so.每个平台文档的
value
字段都需要一个 JSON 字符串,因此请像这样使用json_encode
。
$body['value'] = json_encode([
'project_id' => '1589',
'email' => $user->email,
'name' => $user->name
]);
I had the same issue and it took a min to figure it out because it wasn't clear.我遇到了同样的问题,花了一分钟才弄明白,因为不清楚。
Here is your code snippet with json_encode implemented.这是实现了 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.