发送含糊的发布请求时未找到客户端错误404

时间:2019-08-25 00:30:49

标签: php laravel http laravel-5.4 guzzle

我想使用guzzle向外部api发送发布请求,但出现此错误:
客户端错误: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);

1 个答案:

答案 0 :(得分:1)

这应该可以解决您的问题。每个平台文档的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);