Today I am going to show you some different methods that can be used while using the SOAP API V2 method versus V1. There are a couple of settings that you should be aware of in Magento regarding the API that can impact your API call. You can find these settings here: System > Configuration > Services > Magento Core API.
WS-I Compliance – The WS-I compliant mode uses the same WSDL endpoint as SOAP API V2 does. The key difference is that XML namespaces are used in WS-I compliance mode.
Enable WSDL Cache – When you make an API call into Magento, Magento uses PHP’s SoapServer object. The SoapServer object needs to fetch its own WSDL file to operate, and the generation and fetching of this file can be a time consuming thing. Enabling this feature will improve the API performance.
In our examples, we will be making an API call to retrieve a list of our customers. If successful, the list will return the following:
2014-09-08T19:59:32+00:00 DEBUG (7): stdClass Object
(
[result] => stdClass Object
(
[complexObjectArray] => Array
(
[0] => stdClass Object
(
[customer_id] => 1
[created_at] => 2014-08-22 06:57:15
[updated_at] => 2014-08-21 23:57:16
[store_id] => 1
[website_id] => 1
[created_in] => Blah Store View
[email] => john@yahoo.com
[firstname] => John
[lastname] => Smith
[group_id] => 1
[password_hash] => 'A password hash here'
)
[1] => stdClass Object
(
[customer_id] => 2
[created_at] => 2014-08-22 07:28:19
[updated_at] => 2014-08-22 00:28:19
[store_id] => 1
[website_id] => 1
[created_in] => Blah Store View
[email] => jane@outlook.com
[firstname] => Jane
[lastname] => Doe
[group_id] => 1
[password_hash] => 'A password hash here'
)
)
)
)
SOAP V1 (The old way)
include_once("app/Mage.php"); // Path to your Mage.php file
Mage::init();
$username = 'apiusername'; // Your api username
$password = 'apipassword'; // Your api password
$apiurl = 'https://www.example.com/api/soap/?wsdl'; // The api url that you will connect to
$client = new SoapClient($apiurl);
$session = $client->login($username, $password);
$result = $client->call($session, 'customer.list');
Mage::log($result);
// If you don't need the session anymore
$client->endSession($session);
SOAP V2 (The correct most up-to-date way)
Example 1
include_once("app/Mage.php"); // Path to your Mage.php file
Mage::init();
$username = 'apiusername'; // Your api username
$password = 'apipassword'; // Your api password
$apiurl = 'https://www.example.com/api/v2_soap/?wsdl'; // The api url that you will connect to
$client = new SoapClient($apiurl, array('trace' ==> true, 'connection_timeout' ==> 120));
$session = $client->login(array('username' => $username,'apiKey' => $password));
$sessionId = $session->result; // Set the session ID
// Make sure the session ID is set
if(!$sessionId) {
echo "Oops, the login failed!";
} else {
try {
$result = $client->customerCustomerList(array('sessionId' => $sessionId, 'filters' => null));
Mage::log($result); // Log the results
} catch (Exception $e) {
Mage::logException($e);
}
}
Example 2
include_once("app/Mage.php"); // Path to your Mage.php file
Mage::init();
$username = 'Your Username Here'; // Your api username
$password = 'Your Password Here'; // Your api password
$proxy = new SoapClient('http://www.example.com/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login($username, $password); // TODO : change login and pwd if necessary
$result = $proxy->catalogProductList($sessionId);
var_dump($result);
This post should definitely help you to better understand the structure of making a successful API call. Please leave your comments and questions below.