Batch Lookup

BMCadvertising
Posts: 17
Joined: Wed Apr 04, 2012 2:39 am

Batch Lookup

Postby BMCadvertising » Wed Apr 04, 2012 2:46 am

I'll caveat this question by saying I'm a fairly novice programmer, this may sound simplistic, but I'll give it a go anyway...I installed the PHP API on my website, and have been able to get it to work on incoming clicks...

I'm basically looking for a way to leverage these files to look up a large group of distinct UAs so that I can look at historic volume that has come from our website...is there a good way to write some simple lookup code of some sort?

Thanks!

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: Batch Lookup

Postby kamermans » Wed Apr 04, 2012 8:55 am

Hi,

You can indeed do batch lookups with the PHP API. You should write a quick program to process all the user agents you've collected, something like this:

Code: Select all

// Initialization only needs to happen once
$wurflApiDir = dirname(__FILE__).'/../WURFL';
$resourcesDir = $wurflApiDir.'/../examples/resources';

require_once $wurflApiDir.'/Application.php';

$wurflConfig = new WURFL_Configuration_InMemoryConfig();
$wurflConfig->allowReload(true);
$wurflConfig->wurflFile($resourcesDir.'/wurfl.zip');
$wurflConfig->persistence('file', array('dir' => $resourcesDir.'/storage/persistence'));
$wurflConfig->cache('file', array('dir' => $resourcesDir.'/storage/cache'));
$wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
$wurflManager = $wurflManagerFactory->create();


// Now we can start processing user agents
// We'll take them from the file "uas.txt".  Use "php://stdin" to read from STDIN
$fh = fopen('uas.txt', 'r');
while (($line = fgets($fh, 4096)) !== false) {
        $user_agent = trim($line);
        $requestingDevice = $wurflManager->getDeviceForUserAgent($user_agent);
        $brand = $requestingDevice->getCapability('brand_name');
        $model = $requestingDevice->getCapability('model_name');
        $marketing = $requestingDevice->getCapability('marketing_name');
        echo "$brand $model ($marketing)\n";
}
fclose($fh);
I haven't tested this code, but I'm quite sure it is correct. Anyway, this should give you an idea of how to batch-process user agents.
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!

BMCadvertising
Posts: 17
Joined: Wed Apr 04, 2012 2:39 am

Re: Batch Lookup

Postby BMCadvertising » Wed Apr 04, 2012 12:50 pm

Thanks so much for the quick response...quick followup question:

I actually installed the files that end up putting all the files "Tera-Wurfl" directory on the server...looks like there may be a different set of files for that one, or am I just reading the code incorrectly?

Also, IF I have the right set of files, would I just create a new .php file and toss this code into it, then run it out of my browser?

Thanks again for the help!

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: Batch Lookup

Postby kamermans » Wed Apr 04, 2012 1:00 pm

I see, sorry for the confusion, we have two APIs for PHP, the "PHP API" and the "Database API" (formerly Tera-WURFL). The example I gave was for the PHP API. Here is the example for the Database API (which you are using):

Code: Select all

// Initialization only needs to happen once
require_once realpath(dirname(__FILE__).'/TeraWurfl.php');
$wurflObj = new TeraWurfl();

// Now we can start processing user agents
// We'll take them from the file "uas.txt".  Use "php://stdin" to read from STDIN
$fh = fopen('uas.txt', 'r');
while (($line = fgets($fh, 4096)) !== false) {
        $user_agent = trim($line);
        $wurflObj->getDeviceCapabilitiesFromAgent($user_agent);
        $brand = $wurflObj->getDeviceCapability('brand_name');
        $model = $wurflObj->getDeviceCapability('model_name');
        $marketing = $wurflObj->getDeviceCapability('marketing_name');
        echo "$brand $model ($marketing)\n";
}
fclose($fh);
To use this example, put the code in a file called "batch_lookup.php" in the Tera-WURFL directory, then put all your user agents in a file called "uas.txt".

To run the batch lookup, execute this from the command line (not your web browser):

Code: Select all

php batch_lookup.php > lookup_results.txt
Now you will have a file called lookup_results.txt containing the Brand, Model and Marketing name for each of the user agents in your file. You can, of course, edit the script to output different capabilities as you see fit.
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!

BMCadvertising
Posts: 17
Joined: Wed Apr 04, 2012 2:39 am

Re: Batch Lookup

Postby BMCadvertising » Wed Apr 04, 2012 2:19 pm

Extraordinarily helpful....Thanks!


Who is online

Users browsing this forum: No registered users and 15 guests