【问题标题】:Laravel guzzle issueLaravel 问题
【发布时间】:2017-12-17 01:21:38
【问题描述】:

)

我对 Laravel 5.2 和 Guzzle 6.3 有疑问

这是我发送 POST 到 URL 的代码

    $client = new Client();
    $result = $client->post('https://marketing.webbera.co.uk/form/2', [
    'form_params' => [
        'mauticform_label_welcomeemailwebbera_email' => 'test@test.com',
        'mauticform_label_welcomeemailwebbera_ime' =>'Secret'
    ]
]);

$result = $client->send($result);

这是我遇到的错误

Argument 1 passed to GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, instance of GuzzleHttp\Psr7\Response given, called in /app/Http/Controllers/Registration.php on line 63 and defined

这是第 63 行 $result = $client->send($result);

我不知道有什么问题,所以欢迎任何提示;)

【问题讨论】:

  • $result = $client->send($result); 是干什么用的?您已经在进行 POST。

标签: php laravel-5 guzzle6


【解决方案1】:

你应该检查guzzle 6 docs

您已经通过以下方式提出了发帖请求:

$response = $client->post('http://httpbin.org/post');

send 方法在您发出请求实例时使用。结果是一样的。

$request = new Request('POST', 'http://httpbin.org/post');
$response = $client->send($request);

【讨论】:

    【解决方案2】:

    在 guzzle 的早期版本中 $client->post() 正在创建 Request 对象,该对象需要密码作为 send 方法的参数才能实际发出请求, 但是在 guzzle 6 中 $client->post 会一次性完成这项工作

    所以你可以删除你的最后一行,并使用 $client->post 返回的响应对象

    【讨论】:

      【解决方案3】:

      $client->request 函数负责发送数据,你可以通过 $response->getStatusCode() 控制状态

      use GuzzleHttp\Client;
      $client = new Client();
          $response = $client->request('POST', 'https://marketing.webbera.co.uk/form/2', [
            'form_params' => [
              'mauticform_label_welcomeemailwebbera_email' => 'test@test.com',
              'mauticform_label_welcomeemailwebbera_ime' =>'Secret'
            ],
            'headers' => [
              'Content-Type' => 'application/x-www-form-urlencoded',
            ],
          ]);
      
          if ($response->getStatusCode() != 200) {
            throw new \Exception('Error with status code: ' . $response->getStatusCode() . 'and body: ' . $response->getBody()->getContents());
          }
      

      【讨论】:

        猜你喜欢
        • 2018-01-12
        • 2015-03-02
        • 2023-03-17
        • 2013-04-03
        • 2017-11-29
        • 2018-04-06
        • 2018-09-27
        • 2013-11-08
        • 2017-11-20
        相关资源
        最近更新 更多