API help

Matthew Sikorski

I am trying to create a workflow for purchase orders using the API. In the first part of the process, I am trying to retrieve items based on the item_number using a call like this:

$get_data = callAPI('GET', "items?search=W9084-K&search_field=item_number&limit=1", false);

The call executes properly, but does not return the product information. I don't know if it matters, but most of my 10,000 items have variations (50,000 total). I'm trying to retrieve information on the specific variation referenced by the item_number.

The next part of the process is putting together a call to create a suspended purchase order. I have been able to do that, but rather than including a specific item variation, it just includes the parent item. I looked at the API documentation, but it does not indicate which fields are mandatory. Can I just include a unique item_number? Do I need to include the parent item_id and the variation_id that I want? Do I need to include all the other fields (description, cost, price, etc.)?

I'm sure these are all very basic questions that are obvious to everyone else, but I am new to these API calls. Any suggestions will be greatly appreciated.

 

function callAPI($method, $url, $data){
global $api_url;
global $api_key;

$curl = curl_init();

switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}

// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $api_url . $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('x-api-key:'.$api_key, 'accept: application/json',));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);

// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}

Comments

7 comments

  • Comment author
    Matthew Sikorski

    I'm not sure if it matters, but I am using the downloaded version on my own server.

    0
  • Comment author
    Chris Muench
    • Edited

    It looks ok but start with the below working example and keep modifying it (The code below is tested)

    <?php

    //////////////////////////////////////////////////////////

    $api_base_url = 'http://localhost/phppos/PHP-Point-Of-Sale/index.php/api/v1/';

    $api_key = 'k4koggkc0k40k8w0kkc00gckkcs8kw888sskks0o';

    ///////////////////////////////////////////////////////////







    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $api_base_url.'items');

    curl_setopt($curl, CURLOPT_HTTPHEADER, array(

        'x-api-key:'.$api_key,

    'accept: application/json',

        ));

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 




    $result = json_decode(curl_exec($curl), TRUE);

    echo '<pre>';

    var_dump($result);

    echo '</pre>';
    0
  • Comment author
    Matthew Sikorski

    Thank you for that. The problem was FOLLOWLOCATION (I had set to false).

    Now I just need to figure out what fields I need to pass to create a purchase order with specific variations of items.

    0
  • Comment author
    Matthew Sikorski

    I've been beating my head against this all day and can't get it to create a purchase order with the variation rather than the parent item. Here is the code for my call. Please let me know what I'm doing wrong.


    $po = array('mode' => 'receive',
    'suspended' => 1,
    'is_po' => true,
    'location_id' => 1,
    'employee_id' => 1,
    'register_id' => 1,
    'comment' => 'testpo',
    'supplier_id' => 348);
    $item = array( 'quantity' => 5,
    "item_variation_id"=>62519,
    'item_id'=>6882,
    "name"=>'3" SHORTY CNP 530 YL',
    "item_number"=> "UA/1264660-530-YLG",
    "product_id"=> "UA/1264660-530-YLG",
    "unit_price"=> 19.99,
    "cost_price"=> 8.80,
    "SIZE"=>"YLG",
    "COLOR"=>"530",
    );
    $po['cart_items'][] = $item;
    echo '<pre>'; print_r($po); echo '</pre>';

    echo "Adding PO to the POS...";
    $make_call = callAPI('POST', 'receivings/', json_encode($po));
    $response = json_decode($make_call, true);
    echo '<pre>'; var_dump($response); echo '</pre>';

    0
  • Comment author
    Chris Muench

    You will need to perform debugging steps to figure out the exact error.
    Open up index.php in the phppos root folder:

    FIND:
    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production’);

    REPLACE:
    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : ‘development’);

    open application/config/database.php

    FIND:
    $db['default']['db_debug'] = FALSE;

    REPLACE:
    $db['default']['db_debug'] = TRUE;

    Then see the errors.

    Do NOT forget to undo these changes afterwords.

    0
  • Comment author
    Matthew Sikorski

    There are no errors. The purchase order gets saved, but it has the wrong item. It has the "parent" item rather than the variation that I specified.

    0
  • Comment author
    Chris Muench
    • Edited

    Found a bug and just patched the code. Will be available in 30-60 minutes to download.

    0

Please sign in to leave a comment.