-
Notifications
You must be signed in to change notification settings - Fork 0
/
countries-on-earth.php
64 lines (40 loc) · 1.64 KB
/
countries-on-earth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<html>
<head>
<title>Countries on Earth</title>
</head>
<body>
<h3>Countries on Earth</h3>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
Enter Country Name: <input type="text" name="country_name" size="30"/>
<input type="submit" value="Get Details"/>
</form>
<hr/>
<?php
// Check for an incoming POST request
if ($_POST) {
$countryName = $_POST['country_name'];
//echo 'The user entered: ' . $countryName."<br>";
$queryName = urlencode(str_replace(" ", "", $countryName));
$countriesURL = "https://restcountries.eu/rest/v1/name/".$queryName;
if (!empty($request = json_decode(file_get_contents($countriesURL)))){
$languages = $request[0]->languages;
$langList = '';
foreach ($languages as $lang){
if($langList == ''){
$langList = $lang;
} else {
$langList = $langList.", ".$lang;
}
}
echo "<p>Country Name: ".$request[0]->name."</p>";
echo "<p>Capital: ".$request[0]->capital."</p>";
echo "<p>Region: ".$request[0]->region."</p>";
echo "<p>Population: ".number_format($request[0]->population)."</p>";
echo "<p>Languages: ".$langList;
} else {
echo "<p>No country matches that name. Please try again.</p>";
}
}
?>
</body>
</html>