PDA

View Full Version : vBulletin 4.1 Mobile API



Jamesy
02-12-2010, 03:40 PM
Anyone had a play on this yet? It's been distracting me for a few days learning it now that the documentation (http://www.vbulletin.com/forum/content.php?334-mobile-api) has been released.

I've managed to whip this up. Probably not the most elegant code but it works for testing things:

// Api Key - generated from: admincp/api.php
$Apikey = "";

// Insert the parameters to build the API on: http://www.vbulletin.com/forum/content.php?366-REST-Server-Specific-Methods
$Api_InitParams = array("clientname" => 'apitest', "clientversion" => '1', "platformname" => 'PHP', "platformversion" => '5', "uniqueid" => $Apikey);
ksort($Api_InitParams);
$Api_InitParams = http_build_query($Api_InitParams, '', '&');

$Api_Url = "http://localhost/api.php?api_m=api_init&{$Api_InitParams}";
$vBInfo= json_decode(file_get_contents($Api_Url), true);

// Build Common Array
$ApiParamsArray = array("api_c" => $vBInfo['apiclientid'], "api_s" => $vBInfo['apiaccesstoken'], "api_v" => $vBInfo['apiversion']);
ksort($ApiParamsArray);
$ApiParams = http_build_query($ApiParamsArray, '', '&');

So to get user data for example, include the above and:

// Include the API Data
require_once("api_init.php");

// Get Member info into an array and format for Sign
$UserParamsArray = array("api_m" => "member", "username" => $_GET['username']);
$UserParams = http_build_query($UserParamsArray, '', '&');

// Format full URL Structure
$UserUrlStructure = array_merge($UserParamsArray, $ApiParamsArray);
ksort($UserUrlStructure);
$UserUrlStructure = http_build_query($UserUrlStructure, '', '&');

//Generate the API Sign
$Sign = md5($UserParams.$vBInfo['apiaccesstoken'].$vBInfo['apiclientid'].$vBInfo['secret'] . $Apikey);

//Generate the URL String
$UserUrl = $vBInfo['bburl'] . "api.php?" . $UserUrlStructure . "&api_sig=" . $Sign;
// Get the Data
$UserData = json_decode(file_get_contents($UserUrl), 1);

You're supposed to check the sign on the response but I've forgotten to do that. I got confused by how to sign the api calls at first, it's essentially this:

$Sign = md5($UserParams.$vBInfo['apiaccesstoken'].$vBInfo['apiclientid'].$vBInfo['secret'] . $Apikey);

I've not really played much more than basic things that could be done without using the API, it's still fun to learn though :).

Tom
02-12-2010, 04:20 PM
I've not really checked it out. Nice coding btw :) I'll check it out soon, maybe drift away like you did for a while :)

Want to hide these adverts? Register an account for free!