|
| 1 | +<?php |
| 2 | + |
| 3 | +use kamermans\Command\Command; |
| 4 | + |
| 5 | +require_once __DIR__.'/../vendor/autoload.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * This example demonstrates streaming HTTP User-Agent strings in STDIN |
| 9 | + * and passing them through the ScientiaMobile InSight for Device Analytics |
| 10 | + * product, then outputting JSON objects representing the WURFL device. |
| 11 | + * |
| 12 | + * For information on ScientiaMobile InSight for Device Analytics see |
| 13 | + * http://www.scientiamobile.com/page/wurfl-insight |
| 14 | + */ |
| 15 | + |
| 16 | +$wurfld_server = "localhost:13827"; |
| 17 | +$wurfl_fields = [ |
| 18 | + "device_id", |
| 19 | + "is_mobile", |
| 20 | + "brand_name", |
| 21 | + "model_name", |
| 22 | +]; |
| 23 | + |
| 24 | +// Make sure that wurfld is running |
| 25 | +try { |
| 26 | + $cmd = Command::factory("wurfld-cli") |
| 27 | + ->option("--ping") |
| 28 | + ->run(); |
| 29 | +} catch (Exception $e) { |
| 30 | + Command::echoStdErr(get_class($e).": ".$e->getMessage()."\n"); |
| 31 | + exit(2); |
| 32 | +} |
| 33 | + |
| 34 | +$cmd = Command::factory("wurfld-cli") |
| 35 | + ->option("--host $wurfld_server") |
| 36 | + ->option("--fields ".implode(",", $wurfl_fields)) |
| 37 | + ->setCallback(function($pipe, $data) use ($wurfl_fields) { |
| 38 | + |
| 39 | + if ($pipe === Command::STDERR) { |
| 40 | + Command::echoStdErr($data); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $data_fields = explode("\t", rtrim($data, "\n")); |
| 45 | + $fields_missing = count($wurfl_fields) - count($data_fields); |
| 46 | + if ($fields_missing !== 0) { |
| 47 | + for ($i=0; $i<$fields_missing; $i++) { |
| 48 | + $data_fields[] = ''; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + $device = array_combine($wurfl_fields, $data_fields); |
| 53 | + echo json_encode($device)."\n"; |
| 54 | + |
| 55 | + }, true) |
| 56 | + ->run(STDIN); |
0 commit comments