I am a junior programmer. Recently I've been trying to develop in PHP. I am trying to create a basic page about countries. For some reason I cannot grab the information from this API page. Can anyone give me some tips?
Here is what a sample country array looks like. Ideally I would like to try to grab the "name" attribute.
Here is my HTML and PHP:
<?php
if(!empty($_GET['country'])) {
$country_url = 'https://restcountries.eu/rest/v1/name/' . urlencode($_GET['country']) . '?fullText=true';
$country_json = file_get_contents($country_url);
$country_array = json_decode($country_url, true);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "utf-8" />
<title> PHP Country Practice </title>
</head>
<body>
<form action="">
<input type = "text" name = "country" />
<button type="submit">submit </button>
</form>
<?php
if(!empty($country_array)) {
echo '<p> $country_array['name'] </p>';
}
?>
</body>
</html>
您将返回一个关联数组,因此请使用如下标识符:
$country_array[0]['name']
If it is object use the below code.
echo $country_array[0]->name;
For array use
echo $country_array[0]['name'];
index
, in this case 0
. {}
(tks AbraCadaver ), ie: echo "<p> {$country_array[0]['name']} </p>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.