|
| 1 | +<?php |
| 2 | + |
| 3 | +# Sample code for the WhatIsMyBrowser.com API - Version 2 |
| 4 | +# |
| 5 | +# User Agent Database Search |
| 6 | +# This sample code shows you how to search the database for useragents |
| 7 | +# which match your query. It's not for decoding/parsing user agents, |
| 8 | +# you should use the User Agent Parse API Endpoint instead. |
| 9 | +# |
| 10 | +# It should be used as an example only, to help you get started |
| 11 | +# using the API. This code is in the public domain, feel free to |
| 12 | +# take it an integrate it with your system as you require. |
| 13 | +# Refer to the "LICENSE" file in this repository for legal information. |
| 14 | +# |
| 15 | +# For further documentation, please refer to the Integration Guide: |
| 16 | +# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/ |
| 17 | +# |
| 18 | +# For support, please refer to our Support section: |
| 19 | +# https://developers.whatismybrowser.com/api/support/ |
| 20 | + |
| 21 | +# Your API Key |
| 22 | +# You can get your API Key by following these instructions: |
| 23 | +# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/#introduction-api-key |
| 24 | +$api_key = ""; |
| 25 | + |
| 26 | +# The various search parameters |
| 27 | +# This is a basic search for Safari user agents... but it includes |
| 28 | +# other sample parameters which have been commented out. Change the |
| 29 | +# parameters which get sent to fetch the results you need. |
| 30 | +# |
| 31 | +# You can also use the Web Based form to experiment and see which |
| 32 | +# parameter values are valid: |
| 33 | +# https://developers.whatismybrowser.com/api/docs/v2/sample-code/database-search |
| 34 | + |
| 35 | +$search_params = array( |
| 36 | + "software_name" => "Safari", # "Internet Explorer" "Chrome" "Firefox" |
| 37 | + #"software_version" => "71", |
| 38 | + #"software_version_min" => "64", |
| 39 | + #"software_version_max" => "79", |
| 40 | + |
| 41 | + #"operating_system_name" => "macOS", # "OS X", "Linux", "Android", "iOS" etc |
| 42 | + #"operating_system_version" => "Snow Leopard", # "Vista", "8.2" etc |
| 43 | + |
| 44 | + #"operating_platform" => "iPhone", # "iPhone 5", "Galaxy Gio", "Galaxy Note", "Galaxy S4" |
| 45 | + #"operating_platform_code" => "GT-S5660", |
| 46 | + |
| 47 | + #"software_type" => "browser", # "bot" "application" |
| 48 | + #"software_type_specific" => "web-browser", # "in-app-browser", "analyser" "application" "bot" "crawler" etc |
| 49 | + |
| 50 | + #"hardware_type" => "computer", # "computer" "mobile" "server" |
| 51 | + #"hardware_type_specific" => "computer", # "phone", "tablet", "mobile", "ebook-reader", "game-console" etc |
| 52 | + |
| 53 | + #"layout_engine_name" => "NetFront", # Blink, Trident, EdgeHTML, Gecko, NetFront, Presto |
| 54 | + |
| 55 | + #"order_by" => "times_seen desc", # "times_seen asc" "first_seen_at asc" "first_seen_at desc" "last_seen_at desc" "last_seen_at asc" "software_version desc" |
| 56 | + #"times_seen_min" => 100, |
| 57 | + #"times_seen_max" => 1000, |
| 58 | + #"limit" => 250, |
| 59 | +); |
| 60 | + |
| 61 | + |
| 62 | +# Where will the request be sent to |
| 63 | +$url = "https://api.whatismybrowser.com/api/v2/user_agent_database_search?". http_build_query($search_params); |
| 64 | + |
| 65 | +# -- Set up HTTP Headers |
| 66 | +$headers = [ |
| 67 | + 'X-API-KEY: '.$api_key, |
| 68 | +]; |
| 69 | + |
| 70 | +# -- create a CURL handle containing the settings & data |
| 71 | +$ch = curl_init(); |
| 72 | +curl_setopt($ch,CURLOPT_URL, $url); |
| 73 | +curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); |
| 74 | +curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 75 | + |
| 76 | +# -- Make the request |
| 77 | +$result = curl_exec($ch); |
| 78 | +$curl_info = curl_getinfo($ch); |
| 79 | +curl_close($ch); |
| 80 | + |
| 81 | +# -- Try to decode the api response as json |
| 82 | +$result_json = json_decode($result); |
| 83 | +if ($result_json === null) { |
| 84 | + echo "Couldn't decode the response as JSON\n"; |
| 85 | + exit(); |
| 86 | +} |
| 87 | + |
| 88 | +# -- Check that the server responded with a "200/Success" code |
| 89 | +if ($curl_info['http_code'] != 200) { |
| 90 | + echo "Didn't receive a 200 Success response from the API\n"; |
| 91 | + echo "Instead, there was a ".$curl_info['http_code']." code\n"; |
| 92 | + echo "The message was: ".$result_json->result->message."\n"; |
| 93 | + exit(); |
| 94 | +} |
| 95 | + |
| 96 | +# -- Check the API request was successful |
| 97 | +if ($result_json->result->code != "success") { |
| 98 | + throw new Exception("The API did not return a 'success' response. It said: result: ".$result_json->result.", message_code: ".$result_json->message_code.", message: ".$result_json->message_code); |
| 99 | + exit(); |
| 100 | +} |
| 101 | + |
| 102 | +# Now you have "$result_json" and can store, display or process any part of the response. |
| 103 | + |
| 104 | +# -- print the entire json dump for reference |
| 105 | +var_dump($result_json); |
| 106 | + |
| 107 | +# -- Copy the `user_agents` search results data to a variable for easier use |
| 108 | +$returned_user_agents = $result_json->search_results->user_agents; |
| 109 | + |
| 110 | +foreach ($returned_user_agents as $returned_user_agent) { |
| 111 | + echo $returned_user_agent->user_agent." - seen: ".$returned_user_agent->user_agent_meta_data->times_seen." times"."\n"; |
| 112 | +} |
0 commit comments