Remote Webservice

From Tera-WURFL

(Difference between revisions)
Jump to: navigation, search
(Introduction)
Line 12: Line 12:
==Frontend Files==
==Frontend Files==
-
===TeraWurflRemoteClient.php===
+
===PHP===
-
This file defines '''TeraWurflRemoteClient''' which is a stand-alone Tera-WURFL remote client (available from the '''TeraWurflRemoteClient/''' folder.  The TeraWurflRemoteClient has similar usage to the main Tera-WURFL library, as shown below.
+
For PHP, there is a full-blown client API that mimics the standalone API called '''TeraWurflRemoteClient'''. The remote client (available from the '''TeraWurflRemoteClient/''' folder can be moved to any location, even a different server, and used to query your Tera-WURFL installation. Just include '''TeraWurflRemoteClient.php''' in your scripts and use it as follows:
<pre>
<pre>
require_once('../TeraWurflRemoteClient.php');
require_once('../TeraWurflRemoteClient.php');
$wurflObj = new TeraWurflRemoteClient('http://localhost/Tera-Wurfl/webservice.php');
$wurflObj = new TeraWurflRemoteClient('http://localhost/Tera-Wurfl/webservice.php');
-
$capabilities = array("product_info","fake_capability");
+
// The groups or capabilities you want to use
-
$wurflObj->getCapabilitiesFromAgent(TeraWurflRemoteClient::getUserAgent(),$capabilities);                
+
$capabilities = array("brand_name|model_name|playback|tera_wurfl");
-
if($wurflObj->errors){
+
$wurflObj->getCapabilitiesFromAgent(TeraWurflRemoteClient::getUserAgent(),$capabilities);
-
foreach($wurflObj->errors as $name => $error){
+
echo "You are on a {$wurflObj->getDeviceCapability('brand_name')} {$wurflObj->getDeviceCapability('model_name')}<br/>";
-
echo "$name: $error<br/>";
+
$text = $wurflObj->getDeviceCapability('model_name')? "This device is wireless": "This device is a desktop web browser";
 +
echo $text;
 +
</pre>
 +
===Perl===
 +
<pre>
 +
#!/usr/bin/perl
 +
 
 +
use strict;
 +
use URI;
 +
use LWP::Simple;
 +
use XML::Simple;
 +
 
 +
# Location of Tera-WURFL webservice
 +
my $webservice = URI->new("http://localhost/Tera-Wurfl/webservice.php");
 +
 
 +
# The User Agent you would like to check
 +
my $user_agent = "Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
 +
 
 +
# Capabilities and Groups you want to find
 +
my $search = "brand_name|model_name|marketing_name|is_wireless_device|device_claims_web_support|tera_wurfl";
 +
 
 +
# Build the query String
 +
$webservice->query_form(
 +
"ua" => $user_agent,
 +
"search" => $search
 +
);
 +
 
 +
# Make webservice request
 +
my $xml_response = get $webservice;
 +
# Parse webserver response
 +
my $xml_parser = new XML::Simple(forcearray => 1, keyattr => ['key']);
 +
my $xml_object = $xml_parser->XMLin($xml_response);
 +
# Convert XML Object into Perl Hash
 +
my %capabilities;
 +
foreach(@{$xml_object->{device}[0]->{capability}}){
 +
$capabilities{$_->{name}}=$_->{value};
 +
}
 +
# Make top-level properties available in hash
 +
my %properties = (
 +
"apiVersion", $xml_object->{device}[0]->{apiVersion},
 +
"id", $xml_object->{device}[0]->{id},
 +
"user_agent", $xml_object->{device}[0]->{useragent}
 +
);
 +
 
 +
# Tera-WURFL proccessing is finished, capabilities are available in %capabilities, properties in %properties
 +
 
 +
print "-- Response from Tera-WURFL $properties{apiVersion}\n";
 +
print "-- Device Detected as: $capabilities{brand_name} $capabilities{model_name} $capabilities{marketing_name}\n";
 +
 
 +
my($name,$value);
 +
while(($name,$value) = each(%capabilities)){
 +
print "$name: $value\n";
 +
}
 +
</pre>
 +
===Python===
 +
<pre>
 +
# -*- coding: utf-8 -*-
 +
# Python
 +
 
 +
from urllib import quote
 +
from urllib import urlopen
 +
from xml.dom.minidom import parseString
 +
 
 +
# Location of Tera-WURFL webservice
 +
webservice = "http://localhost/Tera-Wurfl/webservice.php"
 +
 
 +
# The User Agent you would like to check
 +
user_agent = "Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"
 +
 
 +
# Capabilities and Groups you want to find
 +
search = "brand_name|model_name|marketing_name|is_wireless_device|device_claims_web_support|tera_wurfl"
 +
 
 +
querystring = "?ua=" + quote(user_agent) + "&search=" + search
 +
xml_response = urlopen(webservice + querystring).read()
 +
xml_object = parseString(xml_response)
 +
deviceNode = xml_object.firstChild.childNodes[1]
 +
errorsNode = xml_object.firstChild.childNodes[3]
 +
capabilitiesNodes = deviceNode.getElementsByTagName("capability");
 +
 
 +
# Setup Top Level Properties
 +
properties = {
 +
"apiVersion": deviceNode.attributes['apiVersion'].value,
 +
"id": deviceNode.attributes['id'].value,
 +
"useragent": deviceNode.attributes['useragent'].value
 +
}
 +
# Setup Capabilities
 +
capabilities = {}
 +
for capNode in capabilitiesNodes:
 +
# print capNode.toxml() + capNode.attributes['name'].value + capNode.attributes['value'].value + "\n"
 +
capabilities[capNode.attributes['name'].value] = capNode.attributes['value'].value
 +
 
 +
 
 +
# Tera-WURFL processing is finished,  properties and capabilities dictionaries are now filled with data
 +
 
 +
print "Response from Tera-WURFL " + deviceNode.attributes['apiVersion'].value;
 +
for name, value in capabilities.items():
 +
print name + ": " + value
 +
 
 +
</pre>
 +
===ActionScript===
 +
The complete usage example with a Flash FLA is included in the distribution under TeraWurflRemoteClient/examples/ActionScript.
 +
<pre>
 +
btnDetect.addEventListener(MouseEvent.CLICK,startDetection);
 +
function startDetection(event:Event):void {
 +
var xml:XML;
 +
 +
var urlRequest:URLRequest = new URLRequest("http://localhost/Tera-Wurfl/webservice.php?ua=" + escape(txtUA.text) + "&search=" + txtCapabilities.text);
 +
 +
var urlLoader:URLLoader = new URLLoader();
 +
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
 +
urlLoader.load(urlRequest);
 +
}
 +
function urlLoader_complete(evt:Event):void {
 +
txtResult.text = 'Result:\n';
 +
    var xml = new XML(evt.currentTarget.data);
 +
for each( var i:Object in xml..capability){
 +
txtResult.appendText(i.@name + ": " + i.@value + "\n");
 +
}
 +
}
 +
</pre>
 +
===JavaScript===
 +
<pre>
 +
var webserviceURL = 'http://localhost/Tera-Wurfl/webservice.php';
 +
 
 +
function callTeraWurfl(){
 +
document.getElementById("result").innerHTML = 'Loading...';
 +
var fullURL;
 +
var xmlHttpReq = false;
 +
var self = this;
 +
fullURL = webserviceURL + '?ua=' + escape(document.getElementById('user_agent').value) + '&search=' + document.getElementById('capabilities').value;
 +
 +
if(window.XMLHttpRequest) {
 +
self.xmlHttpReq = new XMLHttpRequest();
 +
}else if (window.ActiveXObject) {
 +
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
 +
}
 +
self.xmlHttpReq.open('GET', fullURL, true);
 +
self.xmlHttpReq.onreadystatechange = function() {
 +
if (self.xmlHttpReq.readyState == 4) {
 +
updatepage(self.xmlHttpReq.responseXML.documentElement);
 +
}
 +
}
 +
self.xmlHttpReq.send(null);
 +
}
 +
 
 +
function updatepage(xmlDoc){
 +
var devices = xmlDoc.getElementsByTagName('device');
 +
var capabilities = xmlDoc.getElementsByTagName('capability');
 +
var name, value;
 +
var i;
 +
document.getElementById("result").innerHTML = 'Result from Tera-WURFL ' + devices[0].getAttribute('apiVersion') + '<br/>';
 +
for(i=0;i<capabilities.length;i++){
 +
name = capabilities[i].getAttribute('name');
 +
value = capabilities[i].getAttribute('value');
 +
document.getElementById("result").innerHTML += name + ': ' + value + '<br/>';
}
}
}
}
</pre>
</pre>

Revision as of 11:03, 24 February 2010

Contents

Introduction

Tera-WURFL 2.1.1 introduced a full-featured webservice and remote client for many languages. Some reasons to use the remote webservice to query Tera-WURFL are:

Backend Files

TeraWurflWebservice.php

This file defines the TeraWurflWebservice class and provides the functionality to parse an incoming request, query Tera-WURFL and return the results in XML form.

webservice.php

The webservice.php file takes the raw request from the remote client and decodes it if necessary, then passes it to the TeraWurflWebService class for processing. The file then sends the response back to the client that requested it.

Frontend Files

PHP

For PHP, there is a full-blown client API that mimics the standalone API called TeraWurflRemoteClient. The remote client (available from the TeraWurflRemoteClient/ folder can be moved to any location, even a different server, and used to query your Tera-WURFL installation. Just include TeraWurflRemoteClient.php in your scripts and use it as follows:

require_once('../TeraWurflRemoteClient.php');
$wurflObj = new TeraWurflRemoteClient('http://localhost/Tera-Wurfl/webservice.php');
// The groups or capabilities you want to use
$capabilities = array("brand_name|model_name|playback|tera_wurfl");
$wurflObj->getCapabilitiesFromAgent(TeraWurflRemoteClient::getUserAgent(),$capabilities);
echo "You are on a {$wurflObj->getDeviceCapability('brand_name')} {$wurflObj->getDeviceCapability('model_name')}<br/>";
$text = $wurflObj->getDeviceCapability('model_name')? "This device is wireless": "This device is a desktop web browser";
echo $text;

Perl

#!/usr/bin/perl

use strict;
use URI;
use LWP::Simple;
use XML::Simple;

# Location of Tera-WURFL webservice
my $webservice = URI->new("http://localhost/Tera-Wurfl/webservice.php");

# The User Agent you would like to check
my $user_agent = "Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";

# Capabilities and Groups you want to find
my $search = "brand_name|model_name|marketing_name|is_wireless_device|device_claims_web_support|tera_wurfl";

# Build the query String
$webservice->query_form(
	"ua" => $user_agent,
	"search" => $search
);

# Make webservice request
my $xml_response = get $webservice;
# Parse webserver response
my $xml_parser = new XML::Simple(forcearray => 1, keyattr => ['key']);
my $xml_object = $xml_parser->XMLin($xml_response);
# Convert XML Object into Perl Hash
my %capabilities;
foreach(@{$xml_object->{device}[0]->{capability}}){
	$capabilities{$_->{name}}=$_->{value};
}
# Make top-level properties available in hash
my %properties = (
	"apiVersion", $xml_object->{device}[0]->{apiVersion},
	"id", $xml_object->{device}[0]->{id},
	"user_agent", $xml_object->{device}[0]->{useragent}
);

# Tera-WURFL proccessing is finished, capabilities are available in %capabilities, properties in %properties

print "-- Response from Tera-WURFL $properties{apiVersion}\n";
print "-- Device Detected as: $capabilities{brand_name} $capabilities{model_name} $capabilities{marketing_name}\n";

my($name,$value);
while(($name,$value) = each(%capabilities)){
	print "$name: $value\n";
}

Python

# -*- coding: utf-8 -*-
# Python

from urllib import quote
from urllib import urlopen
from xml.dom.minidom import parseString

# Location of Tera-WURFL webservice
webservice = "http://localhost/Tera-Wurfl/webservice.php"

# The User Agent you would like to check
user_agent = "Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"

# Capabilities and Groups you want to find
search = "brand_name|model_name|marketing_name|is_wireless_device|device_claims_web_support|tera_wurfl"

querystring = "?ua=" + quote(user_agent) + "&search=" + search
xml_response = urlopen(webservice + querystring).read()
xml_object = parseString(xml_response)
deviceNode = xml_object.firstChild.childNodes[1]
errorsNode = xml_object.firstChild.childNodes[3]
capabilitiesNodes = deviceNode.getElementsByTagName("capability");

# Setup Top Level Properties
properties = {
	"apiVersion": deviceNode.attributes['apiVersion'].value,
	"id": deviceNode.attributes['id'].value,
	"useragent": deviceNode.attributes['useragent'].value
}
# Setup Capabilities
capabilities = {}
for capNode in capabilitiesNodes:
#	print capNode.toxml() + capNode.attributes['name'].value + capNode.attributes['value'].value + "\n"
	capabilities[capNode.attributes['name'].value] = capNode.attributes['value'].value


# Tera-WURFL processing is finished,  properties and capabilities dictionaries are now filled with data

print "Response from Tera-WURFL " + deviceNode.attributes['apiVersion'].value;
for name, value in capabilities.items():
	print name + ": " + value

ActionScript

The complete usage example with a Flash FLA is included in the distribution under TeraWurflRemoteClient/examples/ActionScript.

btnDetect.addEventListener(MouseEvent.CLICK,startDetection);
function startDetection(event:Event):void {
	var xml:XML;
	 
	var urlRequest:URLRequest = new URLRequest("http://localhost/Tera-Wurfl/webservice.php?ua=" + escape(txtUA.text) + "&search=" + txtCapabilities.text);
	 
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
	urlLoader.load(urlRequest);
}
function urlLoader_complete(evt:Event):void {
	txtResult.text = 'Result:\n';
    var xml = new XML(evt.currentTarget.data);
	for each( var i:Object in xml..capability){
		txtResult.appendText(i.@name + ": " + i.@value + "\n");
	}
}

JavaScript

var webserviceURL = 'http://localhost/Tera-Wurfl/webservice.php';

function callTeraWurfl(){
	document.getElementById("result").innerHTML = 'Loading...';
	var fullURL;
	var xmlHttpReq = false;
	var self = this;
	fullURL = webserviceURL + '?ua=' + escape(document.getElementById('user_agent').value) + '&search=' + document.getElementById('capabilities').value;
	
	if(window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('GET', fullURL, true);
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepage(self.xmlHttpReq.responseXML.documentElement);
		}
	}
	self.xmlHttpReq.send(null);
}

function updatepage(xmlDoc){
	var devices = xmlDoc.getElementsByTagName('device');
	var capabilities = xmlDoc.getElementsByTagName('capability');
	var name, value;
	var i;
	document.getElementById("result").innerHTML = 'Result from Tera-WURFL ' + devices[0].getAttribute('apiVersion') + '<br/>';
	for(i=0;i<capabilities.length;i++){
		name = capabilities[i].getAttribute('name');
		value = capabilities[i].getAttribute('value');
		document.getElementById("result").innerHTML += name + ': ' + value + '<br/>';
	}
}
Personal tools
Namespaces
Variants
Actions
WURFL DBAPI
Toolbox