Parse User Agents with PHP

You've found the best user agent parser for PHP. It's easy to use our Browser Detection API with PHP to get the most detailed and complete approach to decoding user agents. We are always updating our custom-built user agent parser, so you'll never need to worry about updating your software libraries again! Just send a simple HTTP request and get the result instantly.

Easy to use

Simply sign up and get your API key, then include it when you make API requests.

Here's an example of calling our API with PHP to decode a user agent - this will output the Browser Name, Version, Operating System Name & Version in a readable string - there are lots of extra fields in the response which you can use as well. It also checks if the web browser is up to date and warns you if it isn't.

<?php # -- Specify the user agent to parse: # Alternatively, you can get it from your visitor's request: $_SERVER["HTTP_USER_AGENT"] $user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"; # Paste your API Key into this variable: $api_key = "APIKEYGOESHERE1234567890ABCDEFGH"; # This bit constructs the JSON POST body $post_data = array( "user_agent" => $user_agent, "parse_options" => array(), # You can specify some extra parse options if you need to. ); # -- create a CURL handle containing the settings & data $headers = [ 'X-API-KEY: '.$api_key, ]; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, "https://api.whatismybrowser.com/api/v2/user_agent_parse"); curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($post_data)); curl_setopt($ch,CURLOPT_RETURNTRANSFER, True); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); # -- Make the request $result = curl_exec($ch); $curl_info = curl_getinfo($ch); curl_close($ch); # -- Decode the JSON response from the API $result_json = json_decode($result); # -- Copy the data to some variables for easier use $parse = $result_json->parse; $version_check = $result_json->version_check; # A basic example of outputting their Browser and Operating System # Discover all the available fields here: # https://developers.whatismybrowser.comhttps://developers.whatismybrowser.com/api/features/user-agent-parse#fields-for-plans echo "You are using ".$parse->simple_software_string."\n"; # -- Check if their browser is up to date if ($version_check) { if ($version_check->is_checkable === True) { if ($version_check->is_up_to_date === True) { echo $parse->software_name." is up to date\n"; } else { echo $parse->software_name." is out of date\n"; if ($version_check->latest_version) { echo "The latest version is ".join(".", $version_check->latest_version)."\n"; } if ($version_check->update_url) { echo "You can update here: ".$version_check->update_url."\n"; } } } }

Get the full sample code

The above snippit of code is quite straight-forward and is just intended to get you started. We have more detailed sample code for PHP and many other languages. The sample code also shows you how use the other API End Points - so that you can send hundreds of user agents in a single batch, get all the latest web browser and operating system version numbers, or even download or search our database of over 410 million user agents.

Why Parse User Agents with PHP?

There are lots of reasons you might need to use PHP to parse user agents. The primary one is that you need to warn your visitors that their web browser is out of date and might not work on your site. You might also want to customise your content, Calls to Action, and promotions based on the type of hardware and/or software that they're using.

As well as that, decoding your visitor's user agents can help you understand the general trends on your website - how many visitors access your site on mobile devices each month? What about Smart TVs?! With our API you can find out the answer to these sorts of questions, and use the information to help plan, design, and run your site even better.

Get started now

The API is free to use and easy to set up, so why not get started right now.

Do you have a question? Get in touch! We'd love to help you.