Home > Magento Tutorials > Grabbing Products Cross-Domain with Magento SOAP API

Grabbing Products Cross-Domain with Magento SOAP API

Posted on: 27th Jan 2012 By: Adam Moss 1 Comment

There’s no doubt that Magento’s core API is a useful tool when used in the right way, but it can also be fiddly as hell and sometimes frustrating. My assignment was fairly straightforward, show all products with a SKU (for some reason not all products had one) on a webpage belonging to another website domain.

The two problems are:

  • Keeping API calls to a minimum because it causes huge page load times
  • There are two calls we need, one to get product images and the other gets all other product attributes

So the challenge was finding a way to link the images and the other info so that they can be displayed as one product without incurring to many calls. I combined by code with a JQuery AJAX call to take the strain off the server while the page loads.

1. Collect Product Data

The first step is to connect with your API settings (assuming you’ve already set these up). Loop through the data and create an array of calls which we’ll feed into the multiCall function. This will keep the number of separate calls to a minimum. Within the foreach you may be able to see that my technique is to iterate twice within each loop. This means the each product image will be called on the assigned to the even number, the rest of the data will go to each odd number.

<?php

// Connect to API using SOAP client
$client = new SoapClient('http://www.yourdomain.com/api/soap/?wsdl');
$session = $client->login('username', 'apikey');

// Set up variables
$apicalls = array();
$i = 0;
$ii = 0;

// Optional filters
$filters = array('sku' => array('neq'=>''));

// Initial call to get a list of product IDs
$products = $client->call($session, 'catalog_product.list',array($filters));

// Loop through product IDs and grab attributes & images
foreach ($products as $product){
	$apicalls[$i] = array('catalog_product_attribute_media.list', $product['product_id']);
	$i++;
	$apicalls[$i] = array('catalog_product.info', $product['product_id']);
	$i++;
}

// Use a multiCall to keep total API calls to a minimum
$productinfo = $client->multiCall($session, $apicalls);

?>

The $productinfo array is now filled with a lot of data, but we know that all the images are stored on the even occurrence.

2. Display Product Images & Info

The next stage is to loop through the $productinfo array and again iterate twice within each loop so that we can link the data to a single product (yes this really is the best way I could find of doing this).

<ul>
	<?php while ($ii<count($productinfo)) { ?>
        <li>
            <img src="<?php echo $productinfo[$ii][0]['url'] ?>" alt="" />
            <?php $ii++; ?>
            <a href="<?php echo "http://www.yourdomain.com/".$list[$ii]['url_path'] ?>">
            <h2><?php echo $productinfo[$ii]['name'] ?></h2>
            <?php echo $productinfo[$ii]['description'] ?>
        </li>
        <?php $ii++; ?>
    <?php } ?>
</ul>

You’ll now see a list of your products with whichever data you choose to show, and yes the images come across full size.

3. The AJAX Method

If like me you want to keep load times on the actual page right down then you can call this data with AJAX and let the preloader provide the entertainment. This method is recommended if you’re likely to be getting large amounts of data.

Start by putting the above code on a separate page such as products.php. Now on the page where you want to display your products simply add the following code:

<script type="text/javascript">
$(document).ready(function(){
	$.ajax({
	  url: "products.php", // Enter the name of your page with the above code
	  cache: false,
	  beforeSend:function(){
		$('#results').html('<p class="ajax-loader"><img src="images/loader.gif" /><span>Loading Products</span></p>');
					 },
	  success: function(html){
		$("#results").html(html);
	  }
	});
});
</script>

This assumes you’ve got JQuery loaded on your site, if not you could just use a standard JavaScript AJAX script. That’s all there is to it!

Seriously if anyone has any suggestions on how to improve this code let me know, because it’s a bit ugly – but at least it works! Thanks for reading the Magento Blog at Ecommerce Web Design.

One Response to “ Grabbing Products Cross-Domain with Magento SOAP API ”

  1. Billardzubehör
    #1 | 28th January 2012

    Thanks for this, it’s exactly what I was looking for to get rid of my iFrame solution. One question: how would I set the filter to show products of a specific category? My phone skills are too basic for that. Thanks in advance!

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.