douqin231881 2015-10-08 12:14
浏览 48
已采纳

使用PHP Curl的Lightspeed API

I want to perform a post call with the use of an api. But after some changes I still get the same error and it doesn't get solved/

This is the code I use:

 // 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';
}

I constanly get this error:

HTTP ERROR 415

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

    Unsupported Media Type
Powered by Jetty://

How can I solve this?

  • 写回答

3条回答 默认 最新

  • dongshu4755 2015-10-08 13:12
    关注

    The main issue is that you aren't telling the server what format you are sending, and it's not valid.

    You need to json_encode the array:

    $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'));
    

    Remove http_build_query it's not necessary:

    // array fields var
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    

    Send the header type of the data you are sending:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    

    The end result is:

    $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';
    }
    

    Also, it appears you are missing a field that you'll need to add into your array:

    {"code":"1401","description":"Registration failed, no valid country in company"}
    

    That's unrelated; the (HTTP ERROR 415) should be resolved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?