A project im working on at the moment has the requirement to pull certain stats in from Magento sites that might not be hosted on the same server, Or even have remote MySQL access.
I did some research and decided the explore the Magento API, More specifically SOAP API v2.
The example we will work with should help you show how to load the days sales orders from the Magento API.
As were working with a SOAP API we need to install php-soap extension on both the client machine making the requests and the server where Magento is located. On more distributions this is as simple as apt-get install php5-soap. ( If your using debian ).
Now were almost set up ready to develop some code, However we need to create an API user in Magento and give that user some permissions.
From the Magento AdminHTML section navigate to:
System -> Web Services -> SOAP XML/RPC Users
This section will give us the ability to create a new user. Save the user and now select:
System -> Web Services -> SOAP XML/RPC Roles
Create a new role with some permissions so that we can test the API. In a production usage we should be more restrictive with what a user has access to but for this example select all.
The final step here is to go to the user section again and assign the user we created to the role we created.
Lets create some example code that will pull in some order information from the today, Create a new file anywhere on your system.
The first aspect is the instantiate a SoapClient instance with the API URL of the store:
$client = new SoapClient(“http://www.example.com/api/v2_soap?wsdl\
This initializes client with an instance of the SoapClient and gets us ready to make some calls.
Now we need to authenticate with the Magento instance, Using the SoapClient instance lets make a call to get us authenticated:
$sessionId = $client->__soapCall(‘login’, array($username, $password));
If we entered the correct username / password combination here we should have a connection to Magento and be ready to query some information. Lets make a call to the salesOrderList using a filter:
$complexFilter = array(
‘complex_filter’ => array(
‘key’ => ‘created_at’,
‘value’ => array(‘key’ => ‘gt’, ‘value’ => date(‘Y-m-d 00:00:00′))
$result = $client->salesOrderList($sessionId, $complexFilter);
As I want to filter by an attribute I need to set up a Filter, The Magento API refers to these as complexFilters but in essence im just setting a key => value array where key is the attribute and value contains what I want to filter, In this instance I want to search for all salesOrders where created_at is greater than this morning.
Dumping out the value of result will give you all the orders that have been placed today, When this API call is executed its returning us a similar call to Mage::getModel(‘sales/order’)->getCollection(); We have access to all the orders properties.
The only problem I have faced so far is that the API is a little slow compared to native Magento calls, Yet to have this remote interface packed with as many features as the Magento API has im sure we can live with it.