This tutorial is the second in a series of four to explain you how you can use SOAP to connect to Magento. In the first tutorial we have explained the basics of SOAP and its usage in PHP. Now we will actually start using the Magento API.

Getting the available resources from the Magento API

In the previous tutorial we have seen how we can setup a SOAP client. Once we have initialized SOAP, we can reuse the $soap-object and the $session_id to start communicating with Magento. We first want to know which methods exist in the API. To do this we call the SOAP-method "resources()" with as argument $session_id.

The result is a large array which shows a listing of all the availabe API-methods:

<title></title> <style type="text/css"></style>

...
// Make the API-call
$resources = $soap->resources( $session_id );
?>
<?php if( is_array( $resources ) && !empty( $resources )) { ?>
<?php foreach( $resources as $resource ) { ?>
<h1><?php echo $resource['title']; ?></h1>
Name: <?php echo $resource['name']; ?><br/>
Aliases: <?php echo implode( ',', $resource['aliases'] ); ?>
<table>
<tr>
<th>Title</th>
<th>Path</th>
<th>Name</th>
</tr>
<?php foreach( $resource['methods'] as $method ) { ?>
<tr>
<td><?php echo $method['title']; ?></td>
<td><?php echo $method['path']; ?></td>
<td><?php echo $method['name']; ?></td>
<td><?php echo implode( ',', $method['aliases'] ); ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
<?php } ?>

A simple API-call

    The listing shows us the various API-resources that can be used. These resources do not represent SOAP-methods themselves, but they can be fetched only through the SOAP-method "call()". For instance the list of resources mentions the Country Api that has a method to fetch a list of countries. This method has a path "directory_country.list" and an alias "country.list". To fetch this list of countries, we can use either the path or one the aliases:

    $result = $soap->call( $session_id, 'directory_country.list' );
    $result = $soap->call( $session_id, 'country.list' );

    Again the result is a large array.

    Using arguments in the API-call

      Let's say we want to get the product information of certain product. In the previous examples you may have noticed that we did not need to give additional arguments to the SOAP-method except for the resource-name. If we want to fetch information of a specific product, we need to specify which product.

      The Product API allows us to specify a product through its database ID or its SKU. The database ID is always numeric, the SKU is a string which can be anything.

      $product = $soap->call( $session_id, 'catalog_product.info', 166 );
      $product = $soap->call( $session_id, 'catalog_product.info', 'HTC Touch Diamond' );

      When using dynamic arguments beware of the security element involved. For instance if the Product ID is specified in the URL, you should prevent for exploits by making sure it is a number:

      $product_id = (int)$_GET['product_id'];
      $product = $soap->call( $session_id, 'catalog_product.info', $product_id );

      Giving multiple arguments is just as easy as giving one argument. The clue here is that if we want to pass a single argument to the API-call we can just add that single argument, but if we want to add multiple arguments we need an array:

      $product = $soap->call( $session_id, 'catalog_product.info', array( 166 ));

      We can even use associative arrays and mess around with the ordering of arguments. The only important thing is to use the right value-naming:

      $arguments = array( 'foo' => 'bar', 'product' => 166 );
      $product = $soap->call( $session_id, 'catalog_product.info', $arguments );

      The only missing link when using API-calls which require arguments, is which arguments need to be used. The previous "resources()" call returned a list of the methods but not the arguments. The only way to find out the proper arguments for a given method is through the online Magento Wiki.

      What's next

      At this moment you should have a good view on how to retrieve data from the Magento API. But what about modifying data? In the next two tutorials you will learn more about this and other things. Stay tuned.

      Posted on April 19, 2009

      About the author

      Author Jisse Reitsma

      Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

      Sponsor Yireo

      Looking for a training in-house?

      Let's get to it!

      We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

      Do not miss out on what we say

      This will be the most interesting spam you have ever read

      We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.