POST via php curl

Hi all,
i have worked with API some years ago. I used PHP and curl.
I am now trying to get the API of brickowl working. I didnt had any problem with the GET command. I tried it with https://api.brickowl.com/v1/inventory/list and got back my inventory.

On next step I tried to create a inventory https://api.brickowl.com/v1/inventory/create, but i get back:
{"error":"Access Denied"}
The same when I try PUT.

So my question: I am using the same API-key for the GET, POST, PUT commands. How can it work on GET and why do I get access errors when i try POST, PUT? Could it be wrong settings of the CURL settings?
Or is the error message incorrect and it can be another issue?

Here my CURL code (i already tried some variations...)

$string='{"key":"'.CONSUMER_KEY.'",boid":157052,"quantity":1,"price":1,"condition":"new"}';

// start curl
$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, URL_API."/inventory/create?key=".CONSUMER_KEY."&boid=157052&quantity=1&price=1&condition=new");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);

$result = curl_exec($ch);
curl_close($ch);

return $result;

My tests already where done with:
- $string='{"boid":157052,"quantity":1,"price":1,"condition":"new"}'; // no key= here
- curl_setopt($ch, CURLOPT_URL, URL_API."/inventory/create?key=".CONSUMER_KEY."); // no params, only key
- curl_setopt($ch, CURLOPT_URL, URL_API."/inventory/create); // only URL

URL_API is a constant:
define("URL_API","https://api.brickowl.com/v1");
and CONSUMER_KEY for sure is the API key I got from brickowl

Thanks for all your help in advance, I guess it is only some small error, but as often, like we say in germany: I cannot see the forrest because of all the trees.

Comments

  • 10 Comments sorted by Votes Date Added
  • Usually this is because the arguements for POST do not go in the URL but rather in the body. You may want to google for examples of submitting POST requests for PHP. Also the API only uses GET and POST and not PUT
  • Thanks Lawrence. Knowing that the error message has probably something to do with the wrong connection of the arguments, leads me to make a fast test without using CURL and this worked:

    $url = URL_API."/inventory/create";
    $params=array('key'=>''.CONSUMER_KEY.'','boid'=>'225859-120','quantity'=>'1','price'=>'1','condition'=>'new');
    $options = array(
    'http' => array(
    'header' => "Content-type: application/x-www-form-urlencoded\r\n",
    'method' => 'POST',
    'content' => http_build_query($params),
    ),
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    var_dump($result);

    So CURL is probably something mixing up...
  • And the simple problem was, that the arguments was sent in the wrong format. Here now how it works with CURL, if someone else will have problems:

    $string='{"key":"'.CONSUMER_KEY.'",boid":157052,"quantity":1,"price":1,"condition":"new"}';

    // start curl
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, URL_API."/inventory/create?key=".CONSUMER_KEY."&boid=157052&quantity=1&price=1&condition=new");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($string));

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;

    -> error was on:
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($string)); // http_build_query() was missing
  • Sorry, example above only works with
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    Copy, paste error. I didnt got it run until now with json.
  • That is super nice of you to post the code that worked for others down the line - thanks, Isabel! :-)
  • I will post more when all is ready (functions for all requests, ready to use for php).
    Made the same for bricklink some years ago.

    And this is not Isabel, but here husband Frank. I am supporting here in here managment software.
  • Thank you, Frank, and welcome to BO to you both - you will both love it here, the admins are awesome and VERY responsive! :-)
  • First of all a happy new year to all of you.

    There were some questions coming up during the last days trying to integrate the Brickowl API in my php store managment system. Perhaps someone can help. I tried to find answers in the help-section and the forum but without success. If there is some help available, then its enough to send me a link to the page.

    1. condition ID:
    In the API docu i can see on the "update inventory" following description to the condition ID:
    A Brick Owl condition ID, for example 'new' or 'useda'.
    I tried to find something in the help section about it, because here only 2 condition IDs are described. I could find in the help that there are several other conditions in brickowl, but nothing about their IDs. So how are the other IDs look like? And which IDs are used for Parts and which for Sets?

    2. External_ID and External_ID_1:
    Couldn't find anything what the difference is. I would like to use it to set here the same lot_id I am using in my stock to order the parts, but don't know if external_ID or external_ID_1 is made for it. Or can I give as much external_IDs as wanted and it exists external_ID_1, external_ID_2,...?

    3. Is there a way to get a test-Order? So that I can test the API for the orders?

    Also for Lawrence:
    Can I get API access to catalog? I only will need read access to get the brickowl ID of the parts so that I can upload my stock (which has two IDs part/color).

    Thanks a lot for all your help.
  • 1. Take a look at the "Condition List" section of the API
    2. At the moment they're interchangeable
    3. You can place an order in your own store

    The best thing to do for the last thing is to email us at the link at the bottom of the page to give a bit more information
  • Thanks Lawrence,
    email is out.
    Because of the condition I only found this:
    new, useda, usedg, usedn -> But this seems only to be for parts.
    For sets there exists more conditions. When I enter a Set I can choose
    - New Sealed
    - New Complete
    - New Incomplete
    - Used Complete
    - Used Incomplete
    - Other

    If you don't know the IDs, I could get them when I will get access to the catalog API.
Sign In or Register to comment.