我想使用api执行post调用。但是经过一些修改后,我仍然得到了相同的错误,并且没有得到解决/
这是我使用的代码:
// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://staging-exact-integration.posios.com/PosServer/rest/registration/register');
curl_setopt($ch, CURLOPT_POST, 1);
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'city' => 'Gent',
'company' => 'cedric resto',
'email' => 'cedric@tills.be',
'firstName' => 'Cedric',
'lastName' => 'De Weirt',
'locale' => 'Gent',
'number' => '33',
'password' => '1234',
'phone' => '0476612438',
'referer' => '13c435f9-084a-4e79-866f-8b9800e5c14f',
'street' => 'Testlaan',
'zip'=> '9000')));
// receive server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output;
// further processing
if (strpos($server_output,'201') == true)
{
// created
echo 'Created';
}
else
{
// catch the error(s)
echo 'Error Catch';
}我不断地得到这个错误:
HTTP ERROR 415
Problem accessing /PosServer/rest/registration/register. Reason:
Unsupported Media Type
Powered by Jetty://我怎么才能解决这个问题?
发布于 2015-10-08 13:12:26
主要的问题是,您没有告诉服务器您要发送的格式,而且它是无效的。
您需要对数组进行json_encode:
$payload = json_encode(array(
'city' => 'Gent',
'company' => 'cedric resto',
'email' => 'cedric@tills.be',
'firstName' => 'Cedric',
'lastName' => 'De Weirt',
'locale' => 'Gent',
'number' => '33',
'password' => '1234',
'phone' => '0476612438',
'referer' => '13c435f9-084a-4e79-866f-8b9800e5c14f',
'street' => 'Testlaan',
'zip'=> '9000'));删除http_build_query没有必要:
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);发送要发送的数据的标头类型:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));最终结果是:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://staging-exact-integration.posios.com/PosServer/rest/registration/register');
curl_setopt($ch, CURLOPT_POST, 1);
$payload = json_encode(array(
'city' => 'Gent',
'company' => 'cedric resto',
'email' => 'cedric@tills.be',
'firstName' => 'Cedric',
'lastName' => 'De Weirt',
'locale' => 'Gent',
'number' => '33',
'password' => '1234',
'phone' => '0476612438',
'referer' => '13c435f9-084a-4e79-866f-8b9800e5c14f',
'street' => 'Testlaan',
'zip'=> '9000'));
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// receive server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output;
// further processing
if (strpos($server_output,'201') == true)
{
// created
echo 'Created';
}
else
{
// catch the error(s)
echo 'Error Catch';
}此外,您似乎缺少了一个需要添加到数组中的字段:
{"code":"1401","description":"Registration failed, no valid country in company"}这是不相关的;应该解决(HTTP错误415)。
发布于 2015-10-08 13:08:27
到目前为止,我知道它期望json对象作为post数据。这两个人也是。
添加json标头
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));现在用函数http_build_query更改json_encode。
发布于 2015-10-08 13:16:27
为了解决您的问题,请使用json_encode函数而不是http_build_query()将包含请求参数的数组编码为json格式,而且还需要在header中将内容类型标头定义为application/json,因为有效负载的格式是json格式。
// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://staging-exact-integration.posios.com/PosServer/rest/registration/register');
curl_setopt($ch, CURLOPT_POST, 1);
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'city' => 'Gent',
'company' => 'cedric resto',
'email' => 'cedric@tills.be',
'firstName' => 'Cedric',
'lastName' => 'De Weirt',
'locale' => 'Gent',
'number' => '33',
'password' => '1234',
'phone' => '0476612438',
'referer' => '13c435f9-084a-4e79-866f-8b9800e5c14f',
'street' => 'Testlaan',
'zip'=> '9000')));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
// receive server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output;
// further processing
if (strpos($server_output,'201') == true)
{
// created
echo 'Created';
}
else
{
// catch the error(s)
echo 'Error Catch';
}https://stackoverflow.com/questions/33015493
复制相似问题