Examples
PHP Example Code
Utility Functions to send XML and JSON
// HTTP/Request from PEAR.
include "HTTP/Request.php";
define('APIKEY','00000000000000000000000000000000');
define('HD_SERVER','http://c1.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() {
$options = array('geoip', 'product_info', 'wml_ui', 'chtml_ui', 'xhtml_ui', 'ajax', 'markup', 'cache','display', 'image_format', 'bugs', 'wta', 'security', 'bearer', 'storage', 'object_download','wap_push', 'drm', 'streaming', 'mms', 'j2me', 'sms', 'sound_format', 'flash_lite');
$data = array();
$data['apikey'] = APIKEY;
// Pick up user agent from headers passed to the server
$data['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
// Random ip address. (Yes - its designed to get it totally wrong sometimes)
$data['ipaddress'] = $_SERVER['REMOTE_ADDR'];
$str = "";
// Load up the options array with some random data;
for($i=0; $i < sizeof($options); $i++) {
if (rand(0,1) == 1)
$str .= $options[$i].",";
}
$data['options'] = substr($str, 0, sizeof($str)-2);;
// 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");
}