Skip to content

Instantly share code, notes, and snippets.

@Martin1982
Created August 16, 2012 07:47
Show Gist options
  • Save Martin1982/3368181 to your computer and use it in GitHub Desktop.
Save Martin1982/3368181 to your computer and use it in GitHub Desktop.
A little script to utilize the http://useragentstring.com API
<?php
// Test script for detecting the OS by user agent string
// uses the API from http://www.useragentstring.com
// Do not forget to add caching! Continuously hitting the
// API is bad for performance of both the API and your
// application.
$apiParams = array(
'uas' => $_SERVER['HTTP_USER_AGENT'],
'getJSON' => 'os_name'
);
$apiLocation = "http://www.useragentstring.com/";
$queryString = http_build_query($apiParams);
$requestUrl = $apiLocation . '?' . $queryString;
$jsonResponse = file_get_contents($requestUrl);
$responseData = json_decode($jsonResponse);
echo "<pre>";
echo "Requested api at: " . $requestUrl;
echo "\nYour os is: " . $responseData->$apiParams['getJSON'];
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment