use*_*466 160 php guzzle postman
有没有人知道post
使用JSON 的正确方法Guzzle
?
$request = $this->client->post(self::URL_REGISTER,array(
'content-type' => 'application/json'
),array(json_encode($_POST)));
Run Code Online (Sandbox Code Playgroud)
我收到internal server error
服务器的回复.它适用于Chrome Postman
.
Mic*_*vic 229
对于Guzzle 5和6,您可以这样做:
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('url', [
GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar']
]);
Run Code Online (Sandbox Code Playgroud)
use*_*466 41
对于Guzzle <= 4:
这是一个原始的帖子请求,因此将JSON放入正文解决了问题
$request = $this->client->post($url,array(
'content-type' => 'application/json'
),array());
$request->setBody($data); #set body!
$response = $request->send();
return $response;
Run Code Online (Sandbox Code Playgroud)
Fra*_*oth 41
简单而基本的方式(guzzle6):
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $client->post('http://api.com/CheckItOutNow',
['body' => json_encode(
[
'hello' => 'World'
]
)]
);
Run Code Online (Sandbox Code Playgroud)
要获取响应状态代码和正文的内容,我执行了以下操作:
echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';
Run Code Online (Sandbox Code Playgroud)
dav*_*ash 27
这对我有用(使用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());
Run Code Online (Sandbox Code Playgroud)
Cha*_*ade 25
$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();
Run Code Online (Sandbox Code Playgroud)
Nur*_*uda 16
您可以使用硬编码json
属性作为键,也可以方便地使用GuzzleHttp\RequestOptions::JSON
常量。
这是使用硬编码json
字符串的示例。
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('url', [
'json' => ['foo' => 'bar']
]);
Run Code Online (Sandbox Code Playgroud)
请参阅文档。
小智 7
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')
)
);
Run Code Online (Sandbox Code Playgroud)
根据文档guzzle做json_encode
$client = new \GuzzleHttp\Client(['base_uri' => 'http://example.com/api']);
$response = $client->post('/save', [
'json' => [
'name' => 'John Doe'
]
]);
return $response->getBody();
Run Code Online (Sandbox Code Playgroud)
对于那些正在使用的人来说,$client->request
这是创建 JSON 请求的方法:
$client = new Client();
$res = $client->request('POST', "https://some-url.com/api", [
'json' => [
'paramaterName' => "parameterValue",
'paramaterName2' => "parameterValue2",
]
'headers' => [
'Content-Type' => 'application/json',
]
]);
Run Code Online (Sandbox Code Playgroud)