php 如何使用 Guzzle 以 JSON 格式发送 POST 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22244738/
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 can I use Guzzle to send a POST request in JSON?
提问by user3379466
Does anybody know the correct way to postJSON using Guzzle?
有人知道post使用 JSON的正确方法Guzzle吗?
$request = $this->client->post(self::URL_REGISTER,array(
'content-type' => 'application/json'
),array(json_encode($_POST)));
I get an internal server errorresponse from the server. It works using Chrome Postman.
我得到internal server error了服务器的响应。它使用 Chrome 工作Postman。
采纳答案by user3379466
For Guzzle <= 4:
对于狂饮 <= 4:
It's a raw post request so putting the JSON in the body solved the problem
这是一个原始的发布请求,因此将 JSON 放入正文中解决了问题
$request = $this->client->post($url,array(
'content-type' => 'application/json'
),array());
$request->setBody($data); #set body!
$response = $request->send();
return $response;
回答by Michal Gallovic
回答by Frank Roth
The simple and basic way (guzzle6):
简单而基本的方式(guzzle6):
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $client->post('http://api.com/CheckItOutNow',
['body' => json_encode(
[
'hello' => 'World'
]
)]
);
To get the response status code and the content of the body I did this:
为了获取响应状态代码和正文的内容,我这样做了:
echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';
回答by davykiash
This worked for me (using Guzzle 6)
这对我有用(使用 Guzzle 6)
$client = new Client();
$result = $client->post('http://api.example.com', [
'json' => [
'value_1' => 'number1',
'Value_group' =>
array("value_2" => "number2",
"value_3" => "number3")
]
]);
echo($result->getBody()->getContents());
回答by CharlieJade
$client = new \GuzzleHttp\Client();
$body['grant_type'] = "client_credentials";
$body['client_id'] = $this->client_id;
$body['client_secret'] = $this->client_secret;
$res = $client->post($url, [ 'body' => json_encode($body) ]);
$code = $res->getStatusCode();
$result = $res->json();
回答by Yamen Ashraf
$client = new \GuzzleHttp\Client(['base_uri' => 'http://example.com/api']);
$response = $client->post('/save', [
'json' => [
'name' => 'John Doe'
]
]);
return $response->getBody();
回答by arcos.lwm
This works for me with Guzzle 6.2 :
这适用于 Guzzle 6.2:
$gClient = new \GuzzleHttp\Client(['base_uri' => 'www.foo.bar']);
$res = $gClient->post('ws/endpoint',
array(
'headers'=>array('Content-Type'=>'application/json'),
'json'=>array('someData'=>'xxxxx','moreData'=>'zzzzzzz')
)
);
According to the documentation guzzle do the json_encode
根据文档 guzzle 做 json_encode
回答by Nurul Huda
回答by Tuncay Elvana?a?
Php Version: 5.6
PHP 版本:5.6
Symfony version: 2.3
Symfony 版本:2.3
Guzzle: 5.0
狂饮:5.0
I had an experience recently about sending json with Guzzle. I use Symfony 2.3 so my guzzle version can be a little older.
我最近有一次用 Guzzle 发送 json 的经验。我使用 Symfony 2.3,所以我的 guzzle 版本可能有点旧。
I will also show how to use debug mode and you can see the request before sending it,
我还将展示如何使用调试模式,您可以在发送之前查看请求,
When i made the request as shown below got the successfull response;
当我发出如下所示的请求时,得到了成功的响应;
use GuzzleHttp\Client;
$headers = [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
"Content-Type" => "application/json"
];
$body = json_encode($requestBody);
$client = new Client();
$client->setDefaultOption('headers', $headers);
$client->setDefaultOption('verify', false);
$client->setDefaultOption('debug', true);
$response = $client->post($endPoint, array('body'=> $body));
dump($response->getBody()->getContents());
回答by Tuncay Elvana?a?
Simply use this it will work
只需使用它就可以工作
$auth = base64_encode('user:'.config('mailchimp.api_key'));
//API URL
$urll = "https://".config('mailchimp.data_center').".api.mailchimp.com/3.0/batches";
//API authentication Header
$headers = array(
'Accept' => 'application/json',
'Authorization' => 'Basic '.$auth
);
$client = new Client();
$req_Memeber = new Request('POST', $urll, $headers, $userlist);
// promise
$promise = $client->sendAsync($req_Memeber)->then(function ($res){
echo "Synched";
});
$promise->wait();

