Using Authentication with XML-RPC






Using Authentication with XML-RPC

Problem

You need to pass a username and password along with your XML-RPC request.

Solution

Set the user and pass options and call the xu_rpc_http_concise( ) method:

<?php
require 'utils.php';

// ... other request parameters set here
$user = 'elvis';
$pass = 'the-king';

// make associative array out of these variables
$request = compact('host', 'port', 'uri', 'method', 'args', 'user', 'pass');

// this function makes the XML-RPC request
$result = xu_rpc_http_concise($request);
?>

Discussion

The XML-RPC library does not support HTTP Basic Authentication out of the box. However, the utils.php file mentioned in 14.12, will create the correct HTTP header for you when you pass in user and pass elements to xu_rpc_http_concise( ). For instance:

<?php
require 'utils.php';

// ... other request parameters set here
$user = 'elvis';
$pass = 'the-king';

// make associative array out of these variables
$request = compact('host', 'port', 'uri', 'method', 'args', 'user', 'pass');

// this function makes the XML-RPC request
$result = xu_rpc_http_concise($request);
?>

In this code, the user is elvis and the pass is the-king. These variables are turned into an associative array, $request, along with the other necessary request data. (The other data is omitted for clarity.)

When you pass this information to xu_rpc_http_concise( ), the helper function will base64-encode the data and construct the header for you.

Assuming you have the correct credentials, the rest of the transaction should operate exactly the same as nonauthenticated requests.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows