Recipe 14.13. Using Authentication with XML-RPC


14.13.1. Problem

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

14.13.2. 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); ?>

14.13.3. 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.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net