Examples
API v1.0 - PHP Example Code
Utility Functions to send XML and JSON
These examples assume you have php5, and pear with HTTP_Request installed.
If you have php4, no pear, or want an all in one script then download this.
// HTTP/Request from PEAR.
include "HTTP/Request.php";
define('APIKEY','00000000000000000000000000000000');
define('HD_SERVER','http://api-us1.handsetdetection.com');
function sendjson($data, $url) {
$tmp = json_encode($data);
$req =& new HTTP_Request($url);
$req->addHeader("Content-Type", "application/json");
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addRawPostData($tmp);
$req->sendRequest();
$reply = $req->getResponseBody();
return json_decode($reply, true);
}
function sendxml($data, $url) {
$str = "<?xml version=\"1.0\"?><request>";
foreach($data as $key => $value) {
$str .= "<$key>$value</$key>";
}
$str .= "</request>";
$req =& new HTTP_Request($url);
$req->addHeader("Content-Type", "text/xml");
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addRawPostData($str);
$req->sendRequest();
$reply = $req->getResponseBody();
return simplexml_load_string($reply);
}
Get a list of vendors
// Fetch a list of vendors
function doVendor() {
$data = array();
$data['apikey'] = APIKEY;
$result = sendjson($data, HD_SERVER."/devices/vendors.json");
//$result = sendxml($data, HD_SERVER."/devices/vendors.xml");
}
Get a list of models given the vendor
// Fetch a list of all models for a given vendor
function doModel() {
$data = array();
$data['apikey'] = APIKEY;
// Fetch model information about all nokia devices
$data['vendor'] = "Nokia";
$result = sendjson($data, HD_SERVER."/devices/models.json");
//$result = sendxml($data, HD_SERVER."/devices/models.xml");
}
Detect a device
function doDetect() {
$data = array();
$data['apikey'] = APIKEY;
$data['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
$data['ipaddress'] = $_SERVER['REMOTE_ADDR'];
$data['options'] = "geoip, product_info, display";
// Passing $_SERVER options in is optional.
$data = array_merge ($data, $_SERVER);
$result = sendjson($data, HD_SERVER."/devices/detect.json");
//$result = sendxml($data, HD_SERVER."/devices/detect.xml");
}
API v2.0 - PHP Example Code
Download the PHP WSSE Class
v2.0 returns the same data as v1.0. There are just a few slight differences in the calling conventions from v1.0. Use the same functions above (doDetect, doModel, doVendor) without the API key in the $data array (The ApiKey moves from the payload into the header).
define('APIKEY','keykeykey');
define('HD_SERVER','http://api-us1.handsetdetection.com');
define('SECRET','mysecret');
define('USERNAME','demo@handsetdetection.com');
// HTTP/Request from PEAR.
include "HTTP/Request.php";
function sendjson($data, $url) {
$tmp = json_encode($data);
$wsse = new WSSE(USERNAME, SECRET);
$hdr = $wsse->get_header(true);
$req =& new HTTP_Request($url);
$req->addHeader("Content-Type", "application/json");
$req->addHeader("ApiKey", APIKEY);
foreach ($hdr as $key => $value) {
$req->addHeader($key, $value);
}
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addRawPostData($tmp);
$req->sendRequest();
$reply = $req->getResponseBody();
$tmp = json_decode($reply, true);
return $tmp;
}