用原始数据发送请求 [英] Sending request with guzzle with raw data

查看:86
本文介绍了用原始数据发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送一个看起来像这样的请求.

I want to send a request with guzzle that looks like this.

邮递员请求

事实是,我总是得到

未捕获的异常'GuzzleHttp \ Exception \ ClientException',消息为'客户端错误:POST http://localhost:8080/project/user/login,导致出现400 Bad Request

这是创建和发送请求的代码的一部分:

Here is the part of the code that creates and sends the request:

if (isset ($_REQUEST['username']) && isset($_REQUEST['password'])){
require '../php/vendor/autoload.php';

$myClient = new GuzzleHttp\Client();

$request = $myClient->post("http://localhost:8080/project/user/login");
$request->setBody($_REQUEST['username']. ',' . $_REQUEST['password']);
$feed_response = $request->send();

我已经阅读了大部分的耗材手册,但是却一无所获.

I've read most of the guzzle's manual, but it got me nowhere.

推荐答案

您正在使用哪个版本的Guzzle.使用最新版本.在最新版本的Guzzle中,http POST请求的发送方式为:

What version of Guzzle are you using. Use the latest version. In the latest version of Guzzle, a http POST request is sent as:

$client->request('POST', '/post', [
    'form_params' => [
        'foo' => 'bar',
        'baz' => ['hi', 'there!']
    ]
]);

请参阅以下链接: http://docs. guzzlephp.org/en/latest/request-options.html?#form-params

这篇关于用原始数据发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆