Valor Corporation API Inventory Check with PHP

Date: May 13 2011

Working with XML request via PHP can be pretty easy with the CURL functions built into PHP and make complex tasks seamless. We recently had a client that operated a retail site that need to check their distributors inventory levels consistently through out the day and update their Magento shopping cart software inventory levels. It was a simple solution with some quick PHP implementation.

In reference to the following article “ API – Getting Stock Status” at the companies blog site they had a ASP solution in place to get the real time stock level with the Valorcorp Inventory API. All we need to do was convert that to PHP and feed it into magento.

function Valor_GetCurrentStock($APIkey, $ItemNumber){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, '');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 4);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false );
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false );
	curl_setopt($ch, CURLOPT_POSTFIELDS, "key=$APIkey&item=$ItemNumber");
	$result = curl_exec($ch);
	#Match Item Number
	if(preg_match("/(.*)<\/item_number>/i", $result, $Matches)){
		$RTNarray['itemnumber'] = trim($Matches[1]);
	#Match Stock
	if(preg_match("/(.*)<\/in_stock>/i", $result, $Matches)){
		$RTNarray['stock'] = trim($Matches[1]);
	#Match Description
	if(preg_match("/(.*)<\/short_description>/i", $result, $Matches)){
		$RTNarray['description'] = trim($Matches[1]);
		return $RTNarray;
		return false;


$ArrValorStock = Valor_GetCurrentStock("YOUR-API-KEY-HERE", "Valor Item Number");

The function listed above will return an array of the information based on the Valor Corp Item ID. With magento you can setup up a custom attribute per items to make the relation between your item number and the valor corp item number. Which can then be used to update the stock levels in Magento.

Reference for what is returned from the function:
Valor Corp Item Number : $ArrValorStock['itemnumber']
Valor Corp Stock Level Number : $ArrValorStock['stock']
Valor Corp Description : $ArrValorStock['description']

If you need help doing this for your Magento Store or Any other Shopping Cart System please contact Otel Consulting


Be Sociable, Share!

facebook comments:

Leave a Reply