簡體   English   中英

無法使用PHP訪問API密鑰…知道怎么了嗎?

[英]Unable to Access API Key with PHP…any idea what's up?

我是一名初級程序員。 最近,我一直在嘗試用PHP開發。 我正在嘗試創建有關國家的基本頁面。 由於某些原因,我無法從此API頁面獲取信息。 誰能給我一些提示?

這是樣本國家/地區數組的樣子。 理想情況下,我想嘗試獲取“名稱”屬性。

在此處輸入圖片說明

這是我的HTML和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']

如果是對象,請使用以下代碼。

    echo $country_array[0]->name;

供陣列使用

    echo $country_array[0]['name'];
  1. 您需要訪問正確的數組index ,在本例中為0
  2. 變量不會單引號擴展 ,您需要使用雙引號並將其括在方括號{} (tks AbraCadaver ),即:

echo "<p> {$country_array[0]['name']} </p>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM