While designing a website, we need to know about the device or browser using user agent to provide great user interface. The user agent information is important and plays important role for any website. But we always struggle to get accurate user agent information.
If you’re running a website or working on to develop a website and wants to get details of user agent to make website user friendly. Then you’re here at the right place, you can easily integrate the Userstack API to get the accurate user agent details.
The Userstack API is a real-time, easy to use REST API that take user agent strings as input and output accurate user agent information like detect device, browser and operating system and also result response as JSON data.
In this tutorial you will learn how to consume Userstack API with PHP to get user agent information with example.
Also, read:
So let’s proceed to integrate Userstack API with PHP to get user agent details with example.
Step1: Userstack API Access Key
First we need to sing up to Userstack API to create an user account to get API Access key.
We will pass created API Access Key to make HTTP API request to get user agent details.
https://api.userstack.com/api/detect ? access_key = YOUR_ACCESS_KEY
Step2: Create API Request Query
We will create Userstack API request query with API Access key and user agent string to make HTTP request to get user agent details.
<?php $requestQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'ua' => 'Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1', ]); ?>
Step3: Make API HTTP Request
We will implement functionality to make API HTTP request using request query data. We will get the result data in JSON format and store int
o $responseData
variable.
<?php $ch = curl_init('https://api.userstack.com/detect?' . $requestQuery); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseData = curl_exec($ch); curl_close($ch); ?>
Step4: Display User Agent Information
We will decode result response data with PHP function json_decode()
and store into $result
variable to display details. Then we will use result data and display user agent details.
<?php $result = json_decode($responseData, true); if ($result['browser']['name'] == 'Chrome') { echo "This is a Chrome browser!"; } ?>
Step5: Complete Code to get User Agent Details
Below is the complete code to get the user agent information using Userstack API with PHP.
<?php $requestQuery = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'ua' => 'Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1', ]); $ch = curl_init('https://api.userstack.com/detect?' . $requestQuery); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseData = curl_exec($ch); curl_close($ch); $result = json_decode($responseData, true); if ($result['browser']['name'] == 'Chrome') { echo "This is a Chrome browser!"; } ?>
Below is the user agent response data in JSON format from userstack API.
{ "ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", "type": "browser", "brand": "Apple", "name": "Mac", "url": "https://www.google.com/about/company/", "os": { "name": "macOS 10.14 Mojave", "code": "macos_10_14", "url": "https://en.wikipedia.org/wiki/MacOS_Mojave", "family": "macOS", "family_code": "macos", "family_vendor": "Apple Inc.", "icon": "https://assets.userstack.com/icons/os/macosx.png", "icon_large": "https://assets.userstack.com/icons/os/macosx_big.png" }, "device": { "is_mobile_device": false, "type": "desktop", "brand": "Apple", "brand_code": "apple", "brand_url": "http://www.apple.com/", "name": "Mac" }, "browser": { "name": "Chrome", "version": "71.0.3578.98", "version_major": "71", "engine": "WebKit/Blink" }, "crawler": { "is_crawler": false, "category": null, "last_seen": null } }
Step6: Conclusion
In this tutorial we have explained how to consume Userstack API with PHP to get user agent information. You can also checkout Userstack API documentation to use advance options and features to user agent information.
You may also like:
- IP Geolocation API – IPWHOIS.IO
- How to Integrate IP Geolocation API with PHP
- Scrape Search Result Data using SerpApi with Python
- Get Geocoding using Positionstack API with PHP
- Flight Data using Aviationstack API with PHP
- Get Address Information using Geocode API with PHP
- Review Scraping using ReviewAPI with PHP
- Simple Web Scraping with Zenscrape API using PHP
- Scrape Search Result with Zenserp API using PHP
- Build User Agent Lookup System using Userstack API with PHP
- Build IP Lookup Website using ipapi with PHP
- Build Weather Website with Weatherstack API using PHP
- Get Website Visitors Info using IPStack API with PHP
- Scrape SERP Data using SerpStack API with Python