簡體   English   中英

如何將 php 文件中的 Json 數據解碼為另一個 php 文件?

[英]How to decode Json data from php file into another php file?

I am new in Creating API`s and I have created 2 files.php 1- API_ReadAll.php ( Its an API code that after execution will return Json Result)

示例 執行以下鏈接: http://localhost:8080/To%20work%20on/API_FUNCTIONS/API_ReadAll.php

Output:狀態數據記錄:

[
   {
      STATUS_ID: "14",
      STATUS_CODE: "AP 2013",
      STATUS_DESCRIPTION: "Amazing wheel"
   }, {
      STATUS_ID: "13",
      STATUS_CODE: "AP55.0",
      STATUS_DESCRIPTION: "A23 Powder"
   }, {
      STATUS_ID: "16",
      STATUS_CODE: "AP525.0",
      STATUS_DESCRIPTION: "Power Drink"
   }
]

2- Index.php (它的 php 文件由 HTML 元素和表格組成,以顯示從上面的頁面檢索到的數據)

我試圖做以下事情:

// path to your JSON file

$url = 'http://localhost:8080/To%20work%20on/API_FUNCTIONS/API_ReadAll.php'; 

$json = file_get_contents($url);

$data = json_decode($json,true);

 echo $data->STATUS_ID;

    foreach($data As $display){                     
      echo $display->STATUS_ID."<br>";
      echo $display->STATUS_CODE."<br>";
      echo $display->STATUS_DESCRIPTION."<br>";
    }

Output:

Warning: file_get_contents( http://localhost:8080/To%20work%20on/API_FUNCTIONS/API_ReadAll.php): failed to open stream: No such file or directory in C:\xampp\htdocs\To work on\Temp-Builder\SEED_STATUS.PHP on line 122

Notice: Trying to get property 'STATUS_ID' of non-object in C:\xampp\htdocs\To work on\Temp-Builder\SEED_STATUS.PHP on line 125


Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\To work on\Temp-Builder\SEED_STATUS.PHP on line 125

問題:如何在 Index.php 中獲取和獲取上述 Json。

I'd first of all recommend you to read through the following guide create simple rest api with php (or any other tutorial of your choice), to get into the field of REST Apis with php.

與其將 api 端點作為文件處理,不如使用 curl 對它們執行請求。 然后可以使用 php 在 json_decode function 中的構建將該請求的結果解碼為 json。

為了讓您有一個起點,我將您的方法應用於php.net上的 curl 示例:

   // define api endpoint
   $url = 'http://localhost:8080/To%20work%20on/API_FUNCTIONS/API_ReadAll.php'; 

   // create curl resource
   $ch = curl_init();

   // set url
   curl_setopt($ch, CURLOPT_URL, $url);

   //return the transfer as a string
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   // $output contains the output string
   $output = curl_exec($ch);

   // close curl resource to free up system resources
   curl_close($ch);      

   $json_obj = json_decode($output)

我還注意到,您將“true”作為第二個參數傳遞給json_decode() 這樣,您將收到一個關聯數組,而不是 object。 但在您的示例中,您將其視為 object。

還要注意localhost只能在本地網絡中訪問。 因此,如果這是您的意圖,這里的社區無法訪問您的示例。

祝你好運

-------------------------------------------------- ----------------------

感謝您的時間:

關於您提供給我的鏈接,以了解有關如何構建 API 的更多信息。 我確實從該鏈接中學習並構建了我的 API,並且我成功地實現了我正在尋找的東西。

其次,關於代碼,鏈接的注釋是的,肯定它只能從本地服務訪問,我只是這樣發布,試圖弄清楚這是否是正確的方式。

關於代碼,我剛剛嘗試了您的代碼,但仍然無法顯示以下錯誤:

注意:嘗試在 C:\xampp\htdocs\To work on\Temp-Builder\INDEX.PHP 中獲取非對象的屬性“STATUS_CODE”......

代碼:

    // define api endpoint
    $url = 'http://localhost:8080/To%20work%20on/Temp-Builder/API_FUNCTIONS/API_ReadAllSeed_Status.php'; 

    // create curl resource
     $ch = curl_init();

    // set url
       curl_setopt($ch, CURLOPT_URL, $url);

    //return the transfer as a string
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
        $output = curl_exec($ch);

    // close curl resource to free up system resources
        curl_close($ch);      

         $json_obj = json_decode($output);

        foreach ($json_obj as $display){

            echo $display->STATUS_ID."<br>";
            echo $display->STATUS_CODE."<br>";
            echo $display->STATUS_DESCRIPTION."<br>";
       }

暫無
暫無

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

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