guzzle 超过 6 post 方法无效
guzzle ver 6 post method is not woking
在 postman 工作(application/json 类型的原始格式数据)
guzzle6
url-http://vm.xxxxx.com/v1/hirejob/
{
"company_name":" company_name",
"last_date_apply":"06/12/2015",
"rid":"89498"
}
所以正在创建响应 201
但在 guzzle
$client = new Client();
$data = array();
$data['company_name'] = "company_name";
$data['last_date_apply'] = "06/12/2015";
$data['rid'] = "89498";
$url='http://vm.xxxxx.com/v1/hirejob/';
$data=json_encode($data);
try {
$request = $client->post($url,array(
'content-type' => 'application/json'
),array());
} catch (ServerException $e) {
//getting GuzzleHttp\Exception\ServerException Server error: 500
}
我在 vendor/guzzlehttp/guzzle/src/Middleware.php
上遇到错误
第 69 行
? new ServerException("Server error: $code", $request, $response)
您需要使用 json_encode() JSON_FORCE_OBJECT 标志作为第二个参数。像这样:
$data = json_encode($data, JSON_FORCE_OBJECT);
如果没有 JSON_FORCE_OBJECT 标志,它将创建一个 json 数组,使用方括号表示法而不是大括号表示法。
此外,尝试发送这样的请求:
$request = $client->post($url, [
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => $data
]);
您实际上并没有设置请求正文,但可以说传输 JSON 数据的最简单方法是使用专用请求选项:
$request = $client->post($url, [
'json' => [
'company_name' => 'company_name',
'last_date_apply' => '06/12/2015',
'rid' => '89498',
],
]);
在 postman 工作(application/json 类型的原始格式数据) guzzle6
url-http://vm.xxxxx.com/v1/hirejob/
{
"company_name":" company_name",
"last_date_apply":"06/12/2015",
"rid":"89498"
}
所以正在创建响应 201
但在 guzzle
$client = new Client();
$data = array();
$data['company_name'] = "company_name";
$data['last_date_apply'] = "06/12/2015";
$data['rid'] = "89498";
$url='http://vm.xxxxx.com/v1/hirejob/';
$data=json_encode($data);
try {
$request = $client->post($url,array(
'content-type' => 'application/json'
),array());
} catch (ServerException $e) {
//getting GuzzleHttp\Exception\ServerException Server error: 500
}
我在 vendor/guzzlehttp/guzzle/src/Middleware.php
第 69 行
? new ServerException("Server error: $code", $request, $response)
您需要使用 json_encode() JSON_FORCE_OBJECT 标志作为第二个参数。像这样:
$data = json_encode($data, JSON_FORCE_OBJECT);
如果没有 JSON_FORCE_OBJECT 标志,它将创建一个 json 数组,使用方括号表示法而不是大括号表示法。
此外,尝试发送这样的请求:
$request = $client->post($url, [
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => $data
]);
您实际上并没有设置请求正文,但可以说传输 JSON 数据的最简单方法是使用专用请求选项:
$request = $client->post($url, [
'json' => [
'company_name' => 'company_name',
'last_date_apply' => '06/12/2015',
'rid' => '89498',
],
]);