简体   繁体   中英

Cannot create an article with Bexio API in PHP

I try to post an article to Bexio via the Bexio API: https://docs.bexio.com/resources/article/

There is also a sample for PHP: https://docs.bexio.com/samples/ I updated the scopes in the config.php to allow read and write articles.

I updates the bexioConnector.class.php so i can get Articles (works):

    public function getArticles($urlParams = array()) {
        return $this->call('article', $urlParams);
    }

    public function call($ressource, $urlParams = array(), $postParams = array(), $method = Curl::METHOD_GET) {
        $url = $this->api_url . "/" . $this->org . "/" . $ressource;
        $data = $this->curl->call($url, $urlParams, $postParams, $method, $this->getDefaultHeaders());
        return json_decode($data, true);
    }

So i can use now this code to get all articles (works):

    $bexioProducts = $con->getArticles(array('order_by' => 'id'));

Now i want to create articles with the POST method. So i added this function to the bexioConnector.class.php

    public function postArticle($postParams = array(), $urlParams = array()) {
    return $this->call('article', $urlParams, $postParams, Curl::METHOD_POST);
}

So i use this code to create a product:

  $con->postArticle(array(         
    'intern_code' => "SKU-3214"               
    )
 );

But this ends in an error:

{"error_code":415,"message":"Could not parse the data."}

I have tried a lot but i always get the same error message. What could i have don possibly wrong?

I found the error. I need to encode it as a json first. So i changed my postArticle function:

    public function postArticle($postParams = array(), $urlParams = array()) {
    $json = json_encode($postParams);
    return $this->call('article', $urlParams, $json, Curl::METHOD_POST);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM