From 515392626e0318e9166516f7fdd7a88727006f56 Mon Sep 17 00:00:00 2001 From: AnthorNet Date: Thu, 23 Apr 2015 15:38:47 +0200 Subject: [PATCH] Added PHP Client --- examples/PHP/Client.bat | 3 + examples/PHP/Client.php | 150 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 examples/PHP/Client.bat create mode 100644 examples/PHP/Client.php diff --git a/examples/PHP/Client.bat b/examples/PHP/Client.bat new file mode 100644 index 0000000..dc20b3f --- /dev/null +++ b/examples/PHP/Client.bat @@ -0,0 +1,3 @@ +"php.exe" -f %~dp0\Client.php + +pause \ No newline at end of file diff --git a/examples/PHP/Client.php b/examples/PHP/Client.php new file mode 100644 index 0000000..4754dd0 --- /dev/null +++ b/examples/PHP/Client.php @@ -0,0 +1,150 @@ +', + FILE_APPEND + ); +} + +// UTC +date_default_timezone_set('UTC'); + +echoLog('Starting EDDN Subscribe'); +echoLog(''); + +$context = new ZMQContext(); +$socket = $context->getSocket(ZMQ::SOCKET_SUB); +$socket->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); +$socket->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 600000); + +while (true) +{ + try + { + $socket->connect($relayEDDN); + while (true) + { + $message = $socket->recv(); + + if ($message === false) + { + $socket->disconnect($relayEDDN); + break; + } + + $json = zlib_decode($message); + $array = json_decode($json, true); + $converted = false; + + + // Handle commodity v1 + if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1') + { + echoLog('Receiving commodity-v1 message...'); + echoLog(' - Converting to v2...'); + + $temp = array(); + $temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2'; + $temp['header'] = $array['header']; + + $temp['message'] = array(); + $temp['message']['systemName'] = $array['message']['systemName']; + $temp['message']['stationName'] = $array['message']['stationName']; + $temp['message']['timestamp'] = $array['message']['timestamp']; + + $temp['commodities'] = array(); + + $commodity = array(); + + if(array_key_exists('itemName', $temp['message'])) + $commodity['name'] = $temp['message']['itemName']; + + if(array_key_exists('buyPrice', $temp['message'])) + $commodity['buyPrice'] = $temp['message']['buyPrice']; + if(array_key_exists('stationStock', $temp['message'])) + $commodity['supply'] = $temp['message']['stationStock']; + if(array_key_exists('supplyLevel', $temp['message'])) + $commodity['supplyLevel'] = $temp['message']['supplyLevel']; + + if(array_key_exists('sellPrice', $temp['message'])) + $commodity['sellPrice'] = $temp['message']['sellPrice']; + if(array_key_exists('demand', $temp['message'])) + $commodity['demand'] = $temp['message']['demand']; + if(array_key_exists('demandLevel', $temp['message'])) + $commodity['demandLevel'] = $temp['message']['demandLevel']; + + $temp['commodities'][] = $commodity; + $array = $temp; + unset($temp, $commodity); + + $converted = true; + } + + + // Handle commodity v2 + if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2') + { + if($converted === false) + echoLog('Receiving commodity-v2 message...'); + unset($converted); + + $authorised = false; + $excluded = false; + + if(in_array($array['header']['softwareName'], $authorisedSoftwares)) + $authorised = true; + if(in_array($array['header']['softwareName'], $excludedSoftwares)) + $excluded = true; + + echoLog(' - Software: ' . $array['header']['softwareName'] . ' / ' . $array['header']['softwareVersion']); + echoLog(' - ' . (($authorised === true) + ? 'AUTHORISED' + : (( $excluded === true) ? 'EXCLUDED' : 'UNAUTHORISED') + )); + + if($authorised === true && $excluded === false) + { + // Do what you want with the data... + // Have fun ! + + } + + unset($authorised, $excluded); + } + + echoLog(''); + echoLog(''); + } + } + catch (ZMQSocketException $e) + { + echoLog('ZMQSocketException: ' . $e); + sleep(10); + } +} + +// Exit correctly +exit(0); \ No newline at end of file