【发布时间】:2020-04-01 12:48:56
【问题描述】:
我有一个简单的注册表我想使用 guzzle 将帖子数据发送到外部服务
这是我目前在寄存器控制器中的内容
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$client = new Client();
$response = $client->post('app.salesmanago.pl/api/contact/insert', [
GuzzleHttp\RequestOptions::JSON => [
'allow_redirects' => true,
'timeout' => 2000,
'http_errors' => true,
'headers' => [
'Accept' => 'application/json, application/json',
'Content-Type' => 'application/json',
'clientId' => 'mykey',
'apiKey' => 'mykey',
],
'form_params' => [
'name' => $data['name'],
'email' => $data['email'],
]
]
]);
dd($response);
return $user;
}
现在当我填写表格并发送表格数据时,我收到以下错误
Class 'App\Http\Controllers\Auth\GuzzleHttp\RequestOptions' not found
注意我已经安装了这样的 guzzle:composer require guzzlehttp/guzzle:~6.0
我在这里做错了什么?
【问题讨论】:
-
在
GuzzleHttp前加一个斜线,看起来像\GuzzleHttp\RequestOptions -
@aynber 解决了我的问题,现在没有显示姓名或电子邮件,只是这个ibb.co/ZLvhxjN 为什么?
-
它只会显示其他服务器发回的内容,而不是您发送的内容。试试
dd($response->getBody());看看是否有响应的正文。 -
@aynber 现在我尝试了` $response =$request->getBody(); dd($response);` 我知道了ibb.co/k675Wr5 可以吗?
标签: php laravel http laravel-5 guzzle