Connecting to Magento via XML-RPC

1:42 pm in Web Development by webhank

I have been trying off and on for about a week to get a connection made to Magento’s API via a PHP script.

Magento gives the following code snippet on their site, but I could not get it to work – souring the message boards didn’t really help either.  I am not sure if something was off with my installation instance or what.

  1. $client = new Zend_XmlRpc_Client(‘http://youmagentohost/api/xmlrpc/’);
  2. // If somestuff requires api authentification,
  3. // we should get session token
  4. $session = $client->call(‘login’, array(‘apiUser’, ‘apiKey’));
  5. $client->call(‘call’, array($session, ‘somestuff.method’, array(‘arg1′, ‘arg2′, ‘arg3′)));
  6. $client->call(‘call’, array($session, ‘somestuff.method’, ‘arg1′));
  7. $client->call(‘call’, array($session, ‘somestuff.method’));
  8. $client->call(‘multiCall’, array($session,
  9. array(
  10. array(‘somestuff.method’, ‘arg1′),
  11. array(‘somestuff.method’, array(‘arg1′, ‘arg2′)),
  12. array(‘somestuff.method’)
  13. )
  14. ));
  15. // If you don’t need the session anymore
  16. $client->call(‘endSession’, array($session));

After a bunch of trial and error – scouring the web etc – i was finally able to make a connection with the following script

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
define("XMLRPC_DEBUG", 1);

require_once('../app/Mage.php');
$client = new Zend_XmlRpc_Client('http://www.domain.com/api/xmlrpc');

// If somestuff requires api authentification, we should get session token
$session = $client->call('login', array('user', 'key'));

echo "<pre>";
print_r($session);
echo "</pre>";

// If you don't need the session anymore
$client->call('endSession', array($session));
?>

one strange thing to note is the path to the API – with a trailing slash, i could not get this to work…’

I hope that this helps someone else out.

Share the love:
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC
  • services sprite Connecting to Magento via XML RPC