PHP Examples

CallAPI Function

Use this function to call the API. Make sure you change it to use your user/password.


<?php
    // Method: POST, GET
    // Data: array("param" => "value") ==> index.php?param=value
    function CallAPI($method, $url, $data = false)
    {
        $curl = curl_init();
        switch ($method)
        {
            case "POST":
                curl_setopt($curl, CURLOPT_POST, true);
                if ($data) {
                    $data_string = json_encode($data);
                    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/json',
                        'Content-Length: ' . strlen($data_string)
                    ));
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
                }
                break;
            default:
                if ($data)
                    $url = sprintf("%s?%s", $url, http_build_query($data));
        }
    
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "username:password");
    
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($curl);
    }
?>
    

Product Requests


<?php
    //Get product by sku 
    //https://api2.nalpac.com/Help/Api/GET-api-product-sku
    $product = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product/10006"));
    print $product->Description;
    
    //Get product by upc 
    //https://api2.nalpac.com/Help/Api/GET-api-product_upc
    $product2 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product?upc=782631003079"));
    print $product2->Description;
    
    //Get product by manufacturer number 
    //https://api2.nalpac.com/Help/Api/GET-api-product_manufacturerNumber
    $product3 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product?manufacturerNumber=0307"));
    print $product3->Description;
    
    //Get first 20 products excluding discontinued products
    //https://api2.nalpac.com/Help/Api/GET-api-product_pageNumber_pageSize_modifiedAfter_keyword_manufacturer_excludeDiscontinued
    $product4 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product?pageNumber=1&pageSize=20&excludeDiscontinued=true"));
    foreach($product in $product4->Products)
    {
        print $product->Description;
    }
?>
    

Inventory Requests


<?php
    //Get inventory by sku
    //https://api2.nalpac.com/Help/Api/GET-api-inventory-sku
    $inventory1 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/inventory/10006"));
    print "Sku: $inventory1->Sku Qty:$inventory1->AvailableQuantity<br />";
    
    //Get inventory by upc
    //https://api2.nalpac.com/Help/Api/GET-api-inventory_upc
    $inventory2 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/inventory?upc=782631003079"));
    print "Sku: $inventory2->Sku Qty:$inventory2->AvailableQuantity<br />";
    
    //Get first 20 inventory records
    //https://api2.nalpac.com/Help/Api/GET-api-inventory_pageNumber_pageSize_modifiedAfter
    $inventory3 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/inventory?pageNumber=1&pageSize=20"));
    foreach($inventory3->Inventories as $inventory)
    {
        print "Sku: $inventory->Sku Qty:$inventory->AvailableQuantity<br />";
    }
?>
    

Carrier Requests


<?php
    //Get all available carriers (A shipping option id is required to create an order)
    //https://api2.nalpac.com/Help/Api/GET-api-carrier
    $carrierResults = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/carrier"));
    foreach($carrierResults->Carriers as $carrier)
    {
        print "<br /> Carrier: $carrier->Name <br /> Shipping Options: <br />";
        foreach($carrier->ShippingOptions as $shippingOption)
        {
            print "Id: $shippingOption->Id Description: $shippingOption->Description <br />";
        }
    }
?>

Order Status Requests


<?php
    //Get first page of order statuses
    //https://api2.nalpac.com/Help/Api/GET-api-orderstatus_pageNumber_pageSize_orderedBefore_orderedAfter_shippedBefore_shippedAfter_shipToName
    $orderStatusResults = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/orderstatus"));
    foreach($orderStatusResults->OrderStatuses as $orderStatus)
    {
        print "Order Number: $orderStatus->OrderNumber <br />";
        print "Order Date: $orderStatus->OrderDate <br />";
        print "Status: $orderStatus->Status <br /><br />";
    }

    //Get order status by order number
    //Replace {orderNumber} with the order number to get.
    //https://api2.nalpac.com/Help/Api/GET-api-orderstatus-orderNumber
    $orderStatus = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/orderstatus/{orderNumber}"));
    print "Order Number: $orderStatus->OrderNumber <br />";
    print "Order Date: $orderStatus->OrderDate <br />";
    print "Status: $orderStatus->Status <br /><br />";

    //Get order status by po number
    //Replace {poNumber} with the po number to get
    //https://api2.nalpac.com/Help/Api/GET-api-orderstatus_poNumber
    $orderStatus2 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/orderstatus?poNumber={poNumber}"));
    print "Order Number: $orderStatus2->OrderNumber <br />";
    print "Order Date: $orderStatus2->OrderDate <br />";
    print "Status: $orderStatus2->Status <br /><br />";
?>

Order Requests


<?php
    //Get first page of order details.
    //https://api2.nalpac.com/Help/Api/GET-api-order_pageNumber_orderedBefore_orderedAfter_shippedBefore_shippedAfter_shipToName
    $orderResults = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/order"));
    foreach($orderResults->Orders as $order)
    {
        print "<br />Order Number: $order->OrderNumber <br />";
        print "Order Date: $order->OrderDate <br />";
        foreach($order->OrderLines as $line)
        {
            print "Sku: $line->Sku Quantity: $line->QuantityOrdered Price: $line->TotalPrice <br />";
        }
    }

    //Get order details for a specific order number
    //https://api2.nalpac.com/Help/Api/GET-api-order-orderNumber
    $order = json_decode(CallAPI("GET", "http://localhost:63356/api/order/{orderNumber}"));
    print "<br />Order Number: $order->OrderNumber <br />";
    print "Order Date: $order->OrderDate <br />";
    foreach($order->OrderLines as $line)
    {
        print "Sku: $line->Sku Quantity: $line->QuantityOrdered Price: $line->TotalPrice <br />";
    }

    //Get order details for a specific po number
    //https://api2.nalpac.com/Help/Api/GET-api-order_poNumber
    $order = json_decode(CallAPI("GET", "http://localhost:63356/api/order/{orderNumber}"));
    print "<br />Order Number: $order->OrderNumber <br />";
    print "Order Date: $order->OrderDate <br />";
    foreach($order->OrderLines as $line)
    {
        print "Sku: $line->Sku Quantity: $line->QuantityOrdered Price: $line->TotalPrice <br />";
    }
?>

Create Order


<?php
    $orderData = array(
        "ExternalOrderNumber" => "123456", 
        "PoNumber" => "44444",
        "OrderDate" => "2014-01-10T14:27:12.7682806-05:00",
        "ShippingOptionId" => "128082",
        "DeliveryInstructions" => "Test delivery instructions.",
        "ShippingAddress" => array(
            "Name" => "Test User",
            "Address1" => "123 Test Street",
            "Address2" => "Apt 123",
            "Address3" => "More address info",
            "City" => "TheCity",
            "State" => "MI",
            "ZipCode" => "12345",
            "Country" => "US"
        ),
        "CreateOrderRequestLines" => array(
            array(
                "Sku" => "10006",
                "Quantity" => "1"
            ),
            array(
                "Sku" => "0521",
                "Quantity" => "4"
            )
        )
    );
    CallAPI("POST", "http://localhost:63356/api/order", $orderData)
?>