PHP: casting stdClass Object to a array

When using Soapclient to access a webservice the returned object will be of a stdClass Object.

If you don’t wish to access this as an object, but would find it easier to use it as an array then a quick cast solves this.

/*As an object*/
print_r($soapResult->contents);
/*cast to an array*/
$soapResult = (array)$soapResult;
print_r($soapResult['contents']);

Magento: Starting SOAP (adding a user)

In order to get SOAP up and running you will need to add a web services user in the admin panel.

    First add a role for the user. (Roles are used to limit the permissions of the user)

    • System->Web Services->Roles->Add new role
    • Give the role a name
    • Set permissions for the role
  • Then create the user with these permissions
    • System->Web Services->Users->Add new user
    • Add information about the user (the API key is the password used for SOAP)
    • Under roles bind the user with the previously created role

Magento: Using SOAP to get detailed product information

Here a code solution to using the SOAP interface in magento. This example will get all products (and plenty of information about each product).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$time_start = microtime(true);	
try{
	$devClient = new Soapclient('http://192.168.1.61/magento/index.php/api/?wsdl', array('trace'=>1, 'exceptions'=>1));
	$devSession = $devClient->login('apiuser', 'apipassword');
 
	$productList = $devClient->call($devSession, 'catalog_product.list');
	foreach ($productList as $product){
		$theProduct = array();
		$theProduct['product'] = $product;
		$theProduct['attributeSet'] = current($devClient->call($devSession, 'product_attribute_set.list'));
		$theProduct['info'] = $devClient->call($devSession, 'catalog_product.info', $product['sku']);
		$theProduct['related'] = $devClient->call($devSession, 'catalog_product_link.list', array('related', $product['sku']));
		$theProduct['up_sell'] = $devClient->call($devSession, 'catalog_product_link.list', array('up_sell', $product['sku']));
		$theProduct['cross_sell'] = $devClient->call($devSession, 'catalog_product_link.list', array('cross_sell', $product['sku']));
		$theProduct['grouped'] = $devClient->call($devSession, 'catalog_product_link.list', array('grouped', $product['sku']));
		$theProduct['images'] = $devClient->call($devSession, 'catalog_product_attribute_media.list', $product['sku']);
		$theProduct['tierprice'] = $devClient->call($devSession, 'product_tier_price.info', $product['sku']);
		$theProduct['stock'] = $devClient->call($devSession, 'product_stock.list', $product['sku']);
 
		$allProducts[] = $theProduct;
	}
	echo '$allProducts: <pre>' . print_r($allProducts, true) . '</pre>';
 
}
catch (Exception $e){
	echo 'Error on line '. $e->getLine().' in '. $e->getFile() . $e->getMessage();
}
 
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br/><br/>execution time " . $time;

More info on the SOAP API at Magento wiki