Using the PHP eBay API to Update Stock

A client of ours sells products on eBay as well as their own website and they needed to be able to ensure stock levels were correct on both platforms. This required the use of eBay’s API to manipulate stock on eBay when there was a sale on the website and vice versa. Let’s take a look at how we acheived this.

Updating eBay

This is the XML you need to send eBay to change the stock level of an item, assigned to a variable called $post which we’ll use later.

$post = <<< EOD
<?xml version="1.0" encoding="utf-8"?>
<ReviseInventoryStatusRequest xmlns="urn:ebay:apis:eBLBaseComponents">
    <RequesterCredentials>
    	<eBayAuthToken>[Auth Token]</eBayAuthToken>
    </RequesterCredentials>
    <ErrorLanguage>en_US</ErrorLanguage>
    <WarningLevel>High</WarningLevel>
    <InventoryStatus>
    <ItemID>[Item ID]</ItemID>
    <Quantity>[Quantity]</Quantity>
    </InventoryStatus>
</ReviseInventoryStatusRequest>
EOD;

Pretty simple; you include the Auth Token that identifies your eBay account, the relevant Item (product) ID and the total Quantity you want to declare as in stock.

To send this we use cURL:

$headers = array(
            "X-EBAY-API-COMPATIBILITY-LEVEL: 967",
            "X-EBAY-API-DEV-NAME: [Dev Name]",
            "X-EBAY-API-APP-NAME: [App Name]",
            "X-EBAY-API-CERT-NAME: [Cert Name]",
            "X-EBAY-API-CALL-NAME: ReviseInventoryStatus",
            "X-EBAY-API-SITEID: 3" // 3 for UK
);
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "https://api.ebay.com/ws/api.dll");
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $post);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);

$xml = new SimpleXMLElement($response);

The above code sets the headers which include more identificatory information (which you will get from your eBay developer account) plus the Call Name. In this case we are wanting to change stock, so need to use ReviseInventoryStatus (http://developer.ebay.com/devzone/xml/docs/reference/ebay/ReviseInventoryStatus.html).

We then specify the eBay URL to which this information will be sent, along with the XML we created earlier.

The result of this call is stored in $response as XML. It’s a good idea to store it, plus $post,  in some form so you can track what happened.

Updating Website

Now we can update eBay when we’ve sold something on our site, let’s see how we update our website when something’s sold on eBay.

We need to import orders. This is the XML we need to send:

$post = <<< EOD
<?xml version="1.0" encoding="utf-8"?>
<GetOrdersRequest xmlns="urn:ebay:apis:eBLBaseComponents">
    <RequesterCredentials>
        <eBayAuthToken>[Auth Token]</eBayAuthToken>
    </RequesterCredentials>
    <ErrorLanguage>en_US</ErrorLanguage>
    <WarningLevel>High</WarningLevel>
    <NumberOfDays>1</NumberOfDays>
    <Pagination>
        <EntriesPerPage>100</EntriesPerPage>
        <PageNumber>1</PageNumber>
    </Pagination>
    <OrderRole>Seller</OrderRole>
    <OrderStatus>Completed</OrderStatus>
</GetOrdersRequest>
EOD;

Again we use our Auth Token and this time we specify we want completed orders (OrderStatus) from the past day (NumberOfDays) where we are the seller (OrderRole).

This is the cURL we use to POST to eBay:

$headers = array(
            "X-EBAY-API-COMPATIBILITY-LEVEL: 967",
            "X-EBAY-API-DEV-NAME: [Dev Name]",
            "X-EBAY-API-APP-NAME: [App Name]",
            "X-EBAY-API-CERT-NAME: [Cert Name]",
            "X-EBAY-API-CALL-NAME: GetOrders",
            "X-EBAY-API-SITEID: 3" // 3 for UK
);

$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "https://api.ebay.com/ws/api.dll");
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $post);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);

$xml = new SimpleXMLElement($response);

As you can see it’s very similar to the first code, but the Call Name changes to GetOrders (http://developer.ebay.com/devzone/xml/docs/reference/ebay/GetOrderTransactions.html).

The return is XML which will contain any orders which match the criteria. From here you access the data as such:

foreach($xml->OrderArray->Order as $order){
    $ebayid = $order->OrderID;

    $cart = array();
    $shipping_address = $order->ShippingAddress;

    $name = $shipping_address->Name;
    $addr1 = $shipping_address->Street1;
    $addr2 = $shipping_address->Street2;
    $city = $shipping_address->CityName;
    $county = $shipping_address->StateOrProvince;
    $postcode = $shipping_address->PostalCode;
    $country = $shipping_address->Country;
    $tel = $shipping_address->Phone;

    $ebay_record_number = $order->ShippingDetails->SellingManagerSalesRecordNumber;

    $shippingname = $order->ShippingServiceSelected->ShippingService;
    $shipping_cost = $order->ShippingServiceSelected->ShippingServiceCost;

    $products_total = $order->Subtotal;
    $total = $order->Total;

    $transactionArr = $order->TransactionArray;
    foreach($transactionArr->Transaction as $transaction){
        // Loop through each item in the order

        $email = $transaction->Buyer->Email;

        $item = $transaction->Item;

        $stockcode = (string)$item->SKU;

        if($variation = $transaction->Variation){
            // If the purchase was of a product variation, the stock code/SKU is here
            $stockcode = (string)$variation->SKU;
        }

        $name = (string)$item->Title;
        $grossprice = (float)$transaction->TransactionPrice;
        $quantity = (int)$transaction->QuantityPurchased;
    }
}

As you can see there’s a lot of data that can be accessed but I’ve tried to make the variables self-explanatory, with comments where I deemed necessary.

Now you can update the website stock based on $stockcode and $quantity or even import the new order and/or customer to your own database. Note: $quantity here is how many were purchased, not how many are in stock.

You’ll need to check whether you’ve already imported the order or not before changing stock but apart from that, this will just about get you there.

This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.


We'd love to hear from you!

If you think Bronco has the skills to take your business forward then what are you waiting for?

Get in Touch Today!

Discussion

Write a comment...
  • John Lawrence

    This is exactly the tutorial I was looking for — thank you so much!

  • Catalin

    Thank you, it has helped me a lot. Do you also have the code to change the order status?

    • Chris Antcliff

      You’re welcome. Sorry, no client has asked for that functionality so I’ve never looked into it.

Add a Comment