【问题标题】:Class 'App\Http\Controllers\Auth\GuzzleHttp\RequestOptions' not found找不到类“App\Http\Controllers\Auth\GuzzleHttp\RequestOptions”
【发布时间】: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


【解决方案1】:

您可以使用以下代码以 json 格式发布数据

$client = new Client();
$URI = 'url';
$params['headers'] = ['Content-Type' => 'application/json'];
$params = [
    'json' => [
       'key1' => 'value1',
       'kay2' => 'value2',
     ]
    ];
$response = $client->post($URI, $params);
echo "<pre>";print_r(json_decode($response->getBody(),true));
echo "DONE!";

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 2017-04-11
    • 2019-12-07
    • 2018-04-02
    • 2021-09-25
    • 2016-04-28
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多