fans and counting

api FanBridge API - Connecting

You can reference the examples below to help expedite the process of syncing your application with the FanBridge API platform.

1 - Requesting Access

Place a link in your application to allow the user to login through the FanBridge API Login screen.
The "app_key" parameter is your Application Key: it is unique and distinguishes your product from other apps. You can see yours by going to Labs » Partner Applications »

<!-- Redirects to FanBridge API Login -->
<a href="https://api.fanbridge.com/v3/auth/request_access?app_key=[your_app_key]">Login to FanBridge</a>

2 - Requesting a Token

If the user logs in successfully, FanBridge will redirect the browser to the callback URL provided during the application creation process, along with a "secret" key. At this point, FanBridge will wait for the final request for user access before returning the transaction token, which will enable your application to access the user's info. To complete the process, your application must call to the FanBridge API Request Token URL and provide the "secret" key. If the "secret" key matches, FanBridge will return the transaction token to your application. You can check the response headers to ensure that data has been successfully retrieved.

PHP example using CURL for the call:

<?php

// Call to FanBridge API Request Token URL
if (isset($_GET['secret']))
{
	// Prepares URL and CURL options
	$secret = $_GET['secret'];
	$params = array(
		'secret' => $secret;
	$curl_opts = array(
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_URL => 'https://api.fanbridge.com/v3/auth/request_token',
		CURLOPT_POSTFIELDS => http_build_query($params, null, '&'));

	// Makes the request
	$ch = curl_init();
	curl_setopt_array($ch, $curl_opts);
	$token = curl_exec($ch);

	// Check the request was successfull
	$info = curl_getinfo($ch);
	if ($info['http_code'] != '200')
	{
		// Your error code 
		echo "Error";
	}

	curl_close($ch);

	/* Now you can save $token and $secret (Ie.: in your DB) for calling to the API methods. */
}

3 - Using the API

Once the token for the user is obtained, your application can access all user data indefinitely (i.e., the token will not expire).

The following example will show you how to access the user account info, using the CURL extension in PHP:

<?php

/**
 * Gets user account info from FanBridge.com, using API platform.
 *
 */

// Transport config
$secret = '65crg3841g97b19588ybdf015ea346cc';
$token = 'bc67dy46601uedbfb4259a873ec56b9d';

/**
 * Generates URI signature.
 *
 * @param $args array Parameters to sign (with the token already added).
 * @param $secret string The client secret related to the token.
 * @return string The URI signature.
 */
function generate_signature(array $args, $secret) {

	ksort($args);
	$request_str = '';
	foreach ($args as $key => $value)
	{
		if (is_bool($value))
		{
			$value = (int) $value;
		}
		
		$request_str .= $key . '=' . $value;
	}
	$request_str .= $secret;
	$res = md5($request_str);

	return $res;
}

/**
 * Sends a request to FanBridge API server.
 *
 * @param $package string The name of the FanBridge application package.
 * @param $method string The name of the FanBridge application method.
 * @param $params array Params we want to pass to the FanBridge application method.
 * @param $http_method string The HTTP method used for the request.
 * @return mixed The retrieved data in JSON format.
 */
function send($package, $method, $params = array(), $http_method = 'GET') {

	if ($http_method == 'GET')
	{
		$curl_opts = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_URL => "https://api.fanbridge.com/v3/{$package}/{$method}.json?"
				. http_build_query($params, null, '&'));
	}
	else // POST assumed
	{
		$curl_opts = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_URL => "https://api.fanbridge.com/v3/{$package}/{$method}.json",
			CURLOPT_POSTFIELDS => http_build_query($params, null, '&'));
	}

	$ch = curl_init();
	curl_setopt_array($ch, $curl_opts);
	$res = curl_exec($ch);
	$request_info = curl_getinfo($ch);

	if ($res === false)
	{
		// Connection error, check the content of $request_info for details
	}
	else if ($request_info['http_code'] !== 200)
	{
		// Data error, check the content of $res->description and $res->code for details
	}

	curl_close($ch);

	return $res;
}

// Params that the user wants to send to the method
$params = array();

// Adds the Token
$params['token'] = $token;

// Generates the signature for the URI
$signature = generate_signature($params, $secret);

// Adds the signature to the final URI
$params['signature'] = $signature;

// Makes the request
$package = 'account';
$method = 'me';
$res = send($package, $method, $params);

// Prints FanBridge response
echo '<pre>' . print_r($res);
You can download this example here.