Zend_XmlRpc_Client and session support

I'm in the process of creating (or at least, thinking of creating) an XMLRPC interface to an existing (third party) application at work.

As with 'normal' web applications, I need to use HTTP sessions to support some form of persistence (e.g. to tell who a callee is) across requests.

My fictional API would ideally look something like the following :

$x = new Protocol_Client(....);
$x->authenticate($username, $password, $customer_code);
$y = $x->getStuff();
$x->addStuff(list,of,parameters);

Where getStuff() may return different stuff based on who the user is authenticated as.

Searching the web seemed to produce a total lack of relevant results (the only hits I did find were examples of people using Http Authentication, but this isn't sufficient for me) and I was beginning to wonder if XMLRPC (or REST or SOAP) support cookies (and thereby Http sessions) (as my initial quick demos with the Zend Framework didn't work).

Anyway, using the Zend_XmlRpc libraries it is possible.... In the below code, there is a single "server" script which exposes a single function 'increment' which should increment a number which is stored in the $_SESSION variable each time it's called.

server.php :

require_once('Zend/XmlRpc/Server.php');
/**
 * Increment a number stored in a client's session.
 *
 * @return string a count.
 */
function increment() {
    session_start();
    if(!isset($_SESSION['count'])) {
        $_SESSION['count'] = 0;
    }
    $_SESSION['count']++;
    return $_SESSION['count'];
}
$server = new Zend_XmlRpc_Server();
$server->addFunction('increment', 'test');
echo $server->handle();

The client connects to the server (change the URL as appropriate for you) and calls the 'increment' method 10 times.

client.php:

require_once('Zend/XmlRpc/Client.php');
require_once('Zend/Http/Client.php');
$client = new Zend_XmlRpc_Client("http://orange/david/zend-xmlrpc/server.php");

$http_client = new Zend_Http_Client();
$http_client->setCookieJar();
$client->setHttpClient($http_client);

$server = $client->getProxy('test');
try {
    foreach(range(1, 10) as $count) {
        echo "count is : " . $server->increment() . "\n";
    }
}
catch(Exception $e) {
    echo $e->getMessage();
}

Which gives :

count is : 1
count is : 2
count is : 3
count is : 4
count is : 5
count is : 6
count is : 7
count is : 8
count is : 9
count is : 10

The important bits are the lines involving the Zend_Http_Client, and the turning on of the cookie jar.

Annoyingly, I haven't found any good articles suggesting why I should use Soap or XmlRpc or REST (or something else). I think I'll stick with XmlRpc (possibly out of ignorance).

Technorati Tags:

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is used to make sure you are a human visitor and to prevent spam submissions.
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.