使用 guzzle 发送 post 请求时找不到客户端错误 404

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);