Zend Http 客户端 - 无效的内容类型错误
Zend Http Client - Invalid Content Type Error
我向 ApiGility API 发出 POST 请求时收到无效内容类型错误。
array (size=4) 'type' => string
'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html' (length=54)
'title' => string 'Unsupported Media Type' (length=22) 'status' =>
int 415 'detail' => string 'Invalid content-type specified'
(length=30)
这表明我发送的内容类型不正确。
这是我的代码:
$client = new Client(); //Zend/Http/Client
$client->setUri('http://example.com/api/transfer');
$client->setMethod('POST');
$client->setOptions(
[
'maxredirects' => 0,
'timeout' => 60
]
);
$client->setHeaders(['Accept' => 'application/json', 'Authorization' => 'Bearer 123453c112345c25256ff2dacb8ab212345ace91' ]);
$client->setParameterPost(
[
'total' => 1,
'code' => '0f08c43582f14686aabec4610b332629'
]
);
try {
$response = $client->send();
} catch (\Exception $e) {
throw new \Exception($e);
}
$responseObject = json_decode($response->getBody());
$hydrator = new \Zend\Stdlib\Hydrator\ObjectProperty;
$result = $hydrator->extract($responseObject);
die(var_dump($result));
我无法从手册中解决的问题:http://framework.zend.com/manual/current/en/modules/zend.http.client.html 或实际的客户端代码,是在哪里设置内容类型?
'Content-Type'
应该只是您用于 $client->setHeaders()
的数组中的另一个条目。不过,客户端应默认为 'multipart/form-data'
。
你能做 $client->getRequest()->getHeaders()
的 var_dump()
吗?
我向 ApiGility API 发出 POST 请求时收到无效内容类型错误。
array (size=4) 'type' => string 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html' (length=54)
'title' => string 'Unsupported Media Type' (length=22) 'status' => int 415 'detail' => string 'Invalid content-type specified' (length=30)
这表明我发送的内容类型不正确。
这是我的代码:
$client = new Client(); //Zend/Http/Client
$client->setUri('http://example.com/api/transfer');
$client->setMethod('POST');
$client->setOptions(
[
'maxredirects' => 0,
'timeout' => 60
]
);
$client->setHeaders(['Accept' => 'application/json', 'Authorization' => 'Bearer 123453c112345c25256ff2dacb8ab212345ace91' ]);
$client->setParameterPost(
[
'total' => 1,
'code' => '0f08c43582f14686aabec4610b332629'
]
);
try {
$response = $client->send();
} catch (\Exception $e) {
throw new \Exception($e);
}
$responseObject = json_decode($response->getBody());
$hydrator = new \Zend\Stdlib\Hydrator\ObjectProperty;
$result = $hydrator->extract($responseObject);
die(var_dump($result));
我无法从手册中解决的问题:http://framework.zend.com/manual/current/en/modules/zend.http.client.html 或实际的客户端代码,是在哪里设置内容类型?
'Content-Type'
应该只是您用于 $client->setHeaders()
的数组中的另一个条目。不过,客户端应默认为 'multipart/form-data'
。
你能做 $client->getRequest()->getHeaders()
的 var_dump()
吗?