Using Magento SOAP API to get list of pending orders

SOAP is a XML-based protocol that allows applications to send messages in a standarized way over the Internet. The communication with SOAP requires a SOAP server and a SOAP client. Magento offers a SOAP server that allows you to communicate with Magento on a level that allows you to integrate Magento into your own applications.

To see if your Magento application has SOAP support by browsing to the page http://yourURL/api/?wsdl. It should give you a WSDL endpoint document in XML that define information about the API.

Setting up a SOAP account

Next step is to tell Magento who can access data and what data can be shown. In Magento Admin console, select Webservices and then Roles. Create a new role and select the Resources this role gives access to. If in doubt, give Role "API Full Access" for now. You can tighten this up later.


System > Web Services > SOAP/XML-RPC-Users



System > Web Services > SOAP/XML-RPC-Roles





After creating the role, we need to create an API user. Browse to System, then Webservices and then Roles. The new user needs an username and also an API key (password). These will be used in your API program later.

Next let's write a small program that makes a list of pending orders. This program can be executed on any web server and return list of pending orders.

<?php

    // File name:          
    // Description -     Test Magento Soap Client
    //                     List pending orders    
    // more info: http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.list.html
    
	// Magento login information 
	$mage_url 		= 'http://shopmscii.localhost/api/soap/?wsdl';
	$mage_user 		= 'api-user-name'; 
	$mage_api_key 	= 'zzzzzzzzzz'; 
        
    // SoapClient PHP class provides a client for SOAP 1.1, SOAP 1.2 servers. It can be used in WSDL or non-WSDL mode.
    $soap = new SoapClient($mage_url); 
    
    // Login to Magento 
    $session_id = $soap->login($mage_user, $mage_api_key);

    // possible statuses: pending, processing, complete, cancelled, closed, onhold
    $orderStatus = 'pending';
    
    // if you get a big random string here, then you are on track...
    // echo $session_id;    
    
    $orders_id_array = array();
        
    // sales_order.list
    try {
        $po=$soap->call( $session_id, 
                          'sales_order.list', 
                           array(array('status'=>array('eq'=>$orderStatus))));
    }
    catch(Exception $e) {
        $exceptionMessage = ($e->getMessage());
        output_error_xml(1200);
        exit;
    }
    
    echo "<pre>";
    foreach($po as $order){        
        echo ($order['increment_id'])." - ";        // order number
        echo ($order['customer_id'])." - ";        // customer number
        echo ($order['billing_lastname'])." - ";    // customer last name
        echo ($order['grand_total']);        // grand Total    
        echo "<br/>";
    }        
    echo "</pre>";      	

Ouput:

200000001 - 5 - Customer 1 - 4514.0000
200000002 - 3 - Customer 2 - 1004.0000