Skip to content

Commit c460645

Browse files
Initial commit of PHP sample code
1 parent 62bf58f commit c460645

File tree

5 files changed

+585
-0
lines changed

5 files changed

+585
-0
lines changed

code/software_version_numbers.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
# Sample code for the WhatIsMyBrowser.com API - Version 2
4+
#
5+
# Software Version Numbers
6+
# This sample code provides an example of querying the API
7+
# to get all the latest version numbers for software (ie. Browsers)
8+
# operating systems and plugins.
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+
# Where will the request be sent to
27+
$url = 'https://api.whatismybrowser.com/api/v2/software_version_numbers/all';
28+
29+
# -- Set up HTTP Headers
30+
$headers = [
31+
'X-API-KEY: '.$api_key,
32+
];
33+
34+
# -- create a CURL handle containing the settings & data
35+
$ch = curl_init();
36+
curl_setopt($ch,CURLOPT_URL, $url);
37+
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
38+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
39+
40+
# -- Make the request
41+
$result = curl_exec($ch);
42+
$curl_info = curl_getinfo($ch);
43+
curl_close($ch);
44+
45+
# -- Try to decode the api response as json
46+
$result_json = json_decode($result);
47+
if ($result_json === null) {
48+
echo "Couldn't decode the response as JSON\n";
49+
exit();
50+
}
51+
52+
# -- Check that the server responded with a "200/Success" code
53+
if ($curl_info['http_code'] != 200) {
54+
echo "Didn't receive a 200 Success response from the API\n";
55+
echo "Instead, there was a ".$curl_info['http_code']." code\n";
56+
echo "The message was: ".$result_json->result->message."\n";
57+
exit();
58+
}
59+
60+
# -- Check the API request was successful
61+
if ($result_json->result->code != "success") {
62+
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);
63+
exit();
64+
}
65+
66+
# Now you have "$result_json" and can store, display or process any part of the response.
67+
68+
# -- print the entire json dump for reference
69+
var_dump($result_json);
70+
71+
# -- Copy the data to some variables for easier use
72+
$version_data = $result_json->version_data;
73+
74+
foreach ($version_data as $software_key => $software_version_data) {
75+
76+
echo "Version data for ".$software_key."\n";
77+
78+
foreach ($software_version_data as $stream_code_key => $stream_version_data) {
79+
80+
#var_dump($stream_version_data);
81+
82+
echo " Stream: ".$stream_code_key."\n";
83+
84+
echo "\tThe latest version number for ".$software_key." [".$stream_code_key."] is ".join(".", $stream_version_data->latest_version)."\n";
85+
86+
if ($stream_version_data->update) {
87+
echo "\tUpdate no: ".$stream_version_data->update."\n";
88+
}
89+
90+
if ($stream_version_data->update_url) {
91+
echo "\tUpdate URL: ".$stream_version_data->update_url."\n";
92+
}
93+
94+
if ($stream_version_data->download_url) {
95+
echo "\tDownload URL: ".$stream_version_data->download_url."\n";
96+
}
97+
98+
if ($stream_version_data->release_date) {
99+
echo "\tIt was released: ".$stream_version_data->release_date."\n";
100+
}
101+
102+
if ($stream_version_data->sample_user_agents) {
103+
echo " Some sample user agents with the latest version numbers:\n";
104+
105+
foreach ($stream_version_data->sample_user_agents as $sample_user_agent_group => $sample_user_agents ) {
106+
echo "\tUser agents for ".$sample_user_agent_group." on ".$software_key." [".$stream_code_key."]\n";
107+
108+
foreach ($sample_user_agents as $sample_user_agent) {
109+
echo "\t\t".$sample_user_agent."\n";
110+
}
111+
}
112+
113+
}
114+
}
115+
116+
echo "-------------------------------\n";
117+
118+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
# Sample code for the WhatIsMyBrowser.com API - Version 2
4+
#
5+
# Database Dump Url
6+
# This sample code provides an example of querying the API
7+
# to get the URLs of the latest user agent database dump.
8+
# The database is not for decoding/parsing user agents,
9+
# you should use the User Agent Parse API Endpoint instead.
10+
#
11+
# It should be used as an example only, to help you get started
12+
# using the API. This code is in the public domain, feel free to
13+
# take it an integrate it with your system as you require.
14+
# Refer to the "LICENSE" file in this repository for legal information.
15+
#
16+
# For further documentation, please refer to the Integration Guide:
17+
# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/
18+
#
19+
# For support, please refer to our Support section:
20+
# https://developers.whatismybrowser.com/api/support/
21+
22+
# Your API Key
23+
# You can get your API Key by following these instructions:
24+
# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/#introduction-api-key
25+
$api_key = "";
26+
27+
28+
# choose the format you want to download by uncommenting it
29+
$file_format = "mysql";
30+
#$file_format = "csv";
31+
#$file_format = "txt";
32+
33+
# Where will the request be sent to
34+
$url = "https://api.whatismybrowser.com/api/v2/user_agent_database_dump_url?file_format=". $file_format;
35+
36+
# -- Set up HTTP Headers
37+
$headers = [
38+
'X-API-KEY: '.$api_key,
39+
];
40+
41+
# -- create a CURL handle containing the settings & data
42+
$ch = curl_init();
43+
curl_setopt($ch,CURLOPT_URL, $url);
44+
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
45+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
46+
47+
# -- Make the request
48+
$result = curl_exec($ch);
49+
$curl_info = curl_getinfo($ch);
50+
curl_close($ch);
51+
52+
# -- Try to decode the api response as json
53+
$result_json = json_decode($result);
54+
if ($result_json === null) {
55+
echo "Couldn't decode the response as JSON\n";
56+
exit();
57+
}
58+
59+
# -- Check that the server responded with a "200/Success" code
60+
if ($curl_info['http_code'] != 200) {
61+
echo "Didn't receive a 200 Success response from the API\n";
62+
echo "Instead, there was a ".$curl_info['http_code']." code\n";
63+
echo "The message was: ".$result_json->result->message."\n";
64+
exit();
65+
}
66+
67+
# -- Check the API request was successful
68+
if ($result_json->result->code != "success") {
69+
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);
70+
exit();
71+
}
72+
73+
# Now you have "$result_json" and can store, display or process any part of the response.
74+
75+
# -- print the entire json dump for reference
76+
var_dump($result_json);
77+
78+
# -- Copy the `user_agent_database_dump` data to a variable for easier use
79+
$user_agent_database_dump = $result_json->user_agent_database_dump;
80+
81+
echo "You requested the ".$file_format." data format.\n";
82+
echo "The latest data file contains ".$user_agent_database_dump->num_of_useragents." user agents\n";
83+
echo "You can download it from: ".$user_agent_database_dump->url ."\n";
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)