首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有PHP Curl的Lightspeed

带有PHP Curl的Lightspeed
EN

Stack Overflow用户
提问于 2015-10-08 12:14:09
回答 3查看 595关注 0票数 1

我想使用api执行post调用。但是经过一些修改后,我仍然得到了相同的错误,并且没有得到解决/

这是我使用的代码:

代码语言:javascript
复制
 // 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';
}

我不断地得到这个错误:

代码语言:javascript
复制
HTTP ERROR 415

Problem accessing /PosServer/rest/registration/register. Reason:

    Unsupported Media Type
Powered by Jetty://

我怎么才能解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-08 13:12:26

主要的问题是,您没有告诉服务器您要发送的格式,而且它是无效的。

您需要对数组进行json_encode

代码语言:javascript
复制
$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没有必要:

代码语言:javascript
复制
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

发送要发送的数据的标头类型:

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

最终结果是:

代码语言:javascript
复制
$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';
}

此外,您似乎缺少了一个需要添加到数组中的字段:

代码语言:javascript
复制
{"code":"1401","description":"Registration failed, no valid country in company"}

这是不相关的;应该解决(HTTP错误415)。

票数 0
EN

Stack Overflow用户

发布于 2015-10-08 13:08:27

到目前为止,我知道它期望json对象作为post数据。这两个人也是。

添加json标头

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

现在用函数http_build_query更改json_encode

票数 0
EN

Stack Overflow用户

发布于 2015-10-08 13:16:27

为了解决您的问题,请使用json_encode函数而不是http_build_query()将包含请求参数的数组编码为json格式,而且还需要在header中将内容类型标头定义为application/json,因为有效负载的格式是json格式。

代码语言:javascript
复制
// 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';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33015493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档