Janrain Engage (formerly RPX) PHP Helper Class

Janrain Engage is a system that provides a standardized API for letting users sign in for free in your site using the most common networks (Facebook, MySpace, Google, Windows Live, Yahoo!, OpenID, etc). The API is very well documented, and their PHP example is very simple and understandable.

I got inspired enough to create a simple PHP class based on the steps shown in that example, that allows for easy use of their API.

Source Code

Submitted as a public github gist
The code has sufficient self-documentation (can be removed to save space), but the rest of this page will expand on it.

License

I chose to license it under MIT license (as I understand it, it allows for the code to even be used in proprietary applications), as the code itself is simple and very trivial to implement.

Future-proof

As seen in the source code, the class overloads all function calls. Therefore, every documented and future API call should work (as long as they all follow parameter receiving and JSON data sending).

Extendable

The important methods and fields are protected as opposed to private, so you can create an extended class that can do more than the default, or change the behavior of an API call without having to edit the base class. (See Usage Examples)

Usage Examples

Basic auth_info call

The number of commands to sign the user in become 3 lines!!
$userinfo is an associative array of the API response.
The API key only needs to be specified during initialization.

require_once 'rpx.class.php';
$rpx = new Rpx('your api key here');
$userinfo = $rpx->auth_info(array('token' => $_POST['token']));

auth_info with error handling

The class automatically turns errors into exceptions so you can handle them with the usual try/catch method.
For example, if you use an invalid token (could happen commonly with one-time use tokens), you can do something like this:

try {
    $userinfo = $rpx->auth_info(array('token' => $_POST['token']));
}
catch (RpxException $e) {
    if ($e->getCode() == 2) // Code 2 is 'Data not found'
        // Handle the error here
    else
        // Handle other errors here
}

set_auth_providers Example

All "methods" accept an associative array as input and output an array of the JSON response.

$data = array('providers' => 'facebook,live_id,yahoo');
$rpx->set_auth_providers($data);

Extended class example

In this example, auth_info also returns the field hashedId which is an md5sum of the user's identifier (simple example but should give you an idea of how to extend the class).

require_once 'rpx.class.php';
class Md5Rpx extends Rpx {
    public function auth_info($param) {
        $data = $this->apiCall('auth_info', $param);
        $data['profile']['hashedId'] = md5sum($data['profile']['identifier']);
        return $data;
    }
}
$rpx = new Md5Rpx('apikey');
$repsonse = $rpx->auth_info(array('token' => $_POST['token']));
$profile = $repsonse['profile'];
// This condition will be true:
($profile['hashedId'] == md5sum($profile['identifier']));