如何在 Laravel 上为 Buzz HTTP 客户端的 Post 请求添加表单数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26333416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add form data on Post requests for Buzz HTTP Client on Laravel?
提问by olleh
I'm using Buzz HTTP Clientfor Laravel.
我正在为 Laravel使用Buzz HTTP 客户端。
I have a problem adding form data to my POST requests, since it wasn't specified in it's wiki/documentation.
我在将表单数据添加到我的 POST 请求时遇到问题,因为它没有在它的 wiki/documentation 中指定。
Listed below are the two ways of sending requests.
下面列出了两种发送请求的方式。
Example 1:
示例 1:
$response = Buzz::post('http://api.website.com/login');
//how do I add a "username", and "password" field in my POST request?
echo $response;
echo $response->getContent;
Example 2:
示例 2:
$request = new Buzz\Message\Request('POST', '/', 'http://google.com');
$response = new Buzz\Message\Response();
//how do I add a "username", and "password" field in my POST request?
$client = new Buzz\Client\FileGetContents();
$client->send($request, $response);
echo $request;
echo $response;
回答by Logan Bailey
The answer here is going to really depend on what the API expects. Lets assume, the API expects the password and username sent as JSON in the content of the request. The example http request would look something like:
这里的答案将真正取决于 API 的期望。让我们假设,API 期望在请求的内容中以 JSON 形式发送密码和用户名。示例 http 请求类似于:
POST /login HTTP/1.1
Content-Type: application/json
{
"username": "bugsBunny",
"password": "wh4tsUpD0c"
}
To do this with Buzz, this should work:
要使用 Buzz 做到这一点,这应该有效:
$jsonPayload = json_encode([
‘username' => ‘bugsBunny',
‘password' => ‘wh4tsUpD0c
]);
$headers = ['Content-Type', 'application/json'];
$response = Buzz::post('http://api.website.com/login', $headers, $jsonPayload);
If you're attempting to submit a form on a given website, you shouldn't use the above method. Instead use Buzz's built in form method which will attach the correct headers.
如果您尝试在给定网站上提交表单,则不应使用上述方法。而是使用 Buzz 的内置表单方法,它将附加正确的标题。
use Buzz\Message\Form;
$request = new Form(Form::METHOD_POST, ‘login', ‘api.website.com');
$request->setFields([
‘username' => ‘bugsBunny',
‘password' => ‘wh4tsUpD0c'
]);
$response = new Buzz\Message\Response();
$client = new Buzz\Client\Curl();
$client->send($request, $response);
On a side note, I'd suggest not using this library. The library is, as you stated, Laravel integration for Buzz. The issue here is, the author should have made buzz a dependency listed in composer, rather than include the Buzz source directly. This prevents updates to Buzz from making their way into this project. You can see on the actual Buzz repo, the last commit was 29 days ago. Also if another package is using Buzz and including it correctly by composer, composer would install both packages. But when an instance of Buzz was created, you couldn't be certain which version was being loaded. You should just use Buzz, which can be found on packagist.
附带说明一下,我建议不要使用这个库。正如您所说,该库是 Buzz 的 Laravel 集成。这里的问题是,作者应该让 buzz 成为 Composer 中列出的依赖项,而不是直接包含 Buzz 源。这会阻止 Buzz 的更新进入该项目。您可以在实际的Buzz 存储库中看到,最后一次提交是 29 天前。此外,如果另一个包正在使用 Buzz 并由 composer 正确包含它,composer 将安装这两个包。但是,当创建 Buzz 实例时,您无法确定正在加载哪个版本。您应该只使用 Buzz,它可以在packagist上找到。
// assuming $headers and $jsonPayload are the same as in previous example.
$browser = new Buzz\Browser();
$response = $browser->post('http://api.website.com/login', $headers, $jsonPayload);
回答by olleh
It was foolish of me to not read the code first before asking.
在询问之前不先阅读代码是我的愚蠢。
The form data is actually pased on the third parameter for the function. Though it accepts strings only so don't forget to json encode your data.
表单数据实际上传递给函数的第三个参数。虽然它只接受字符串,所以不要忘记对你的数据进行 json 编码。
Buzz Class
public function post($url, $headers = array(), $content = '')
{
....
....
}
Buzz::post($url, array(), json_encode(array('Username'=>'usernamexx','Password'=>'p@$$w0rD')) );