2017-09-02 74 views
1

我是新來的Web服務,並找到一個api.php文件,我能夠連接到我的MSSQL服務器和檢索信息沒有問題,所以這是偉大的。我現在想要將我正在拖動的數據呈現在HTML頁面中。我不知道如何做到這一點,並已經做了大量的閱讀,試圖找出答案。我已經運行了通過JSON驗證器返回的數據並進行了驗證。任何人都可以給我建議如何使這個顯示在網頁上(這是爲了證明概念)。我在IIS ......我試過很多東西,最新的是:JSON的結構和返回

<!doctype html> 
    <html> 
     <head> 
    </head> 
     <body> 
      <?php 
      $json = file_get_contents('https://myurl.com/rest/api.php/content?filter=category,cs,10&columns=id_cr,title'); 
      $data = json_decode($json); 
      echo "<pre>"; 
       print_r($data); 
      echo "</pre>"; 
      exit; 
     ?> 
     </body> 
</html> 

這裏是JSON數據輸出的外觀 - 不知道如何訪問數據的變量此格式中......

{ 
"content_data": { 
    "columns": [ 
    "id", 
    "title" 
    ], 
    "records": [ 
    [ 
    41901, 
    "Resources for Educational Excellence" 
    ], 
    [ 
    44230, 
    "The Khan Academy" 
    ], 
    [ 
    145925, 
    "Educating Your Child at Home" 
    ], 
    ] 
} 
} 

:::固定::: 我終於得到了我的代碼工作!所以,最終的代碼看起來像這樣...

<!doctype html> 
    <html> 
     <head></head> 
     <body> 
      <?php 
       $json = file_get_contents('http://myurl.com/rest_api/api_test.php/content_data?filter=category,cs,10&columns=id,title'); 
       $data = json_decode($json, TRUE); 
       foreach ($data['content_data']['records'] as $rec) { 
        echo $rec[0] .' '. $rec[1] . '<br />'; 
       } 
      ?> 
    </body> 
</html> 

所以問題結束了,當我有我的HTTPS服務器上我api_data,我不能對任何結果拉回到我的PHP頁面,但當我將api_test.php文件移動到http服務器時,它像一個魅力一樣工作。任何人都可以解釋爲什麼發生?謝謝!

而且,我只是想感謝大家對我的幫助,這對我來說是艱難的項目,但我學到了很多東西,下次應該更容易:)

+0

你們是不是要顯示它內部的表? – chad

+0

我一直在想,這將工作或只是一個標題列表...我希望它是在一個響應頁面上... – user1255154

回答

1

在PHP中,你將能夠訪問如果將json_decode的第二個參數設置爲TRUE以使其成爲所有數組,則數據更容易。

與您的數據意味着:

$json = '{ 
"content_data": { 
    "columns": [ 
    "id", 
    "title" 
    ], 
    "records": [ 
    [ 
    41901, 
    "Resources for Educational Excellence" 
    ], 
    [ 
    44230, 
    "The Khan Academy" 
    ], 
    [ 
    145925, 
    "Educating Your Child at Home" 
    ], 
    ] 
} 
}'; 

你會再能得到這樣的數據:

$data = json_decode($json, TRUE); 

foreach($data['content_data']['records'] as $record) 
{ 
    echo '<p>' . $record[0] . '<br /> 
    ' . $record[1] . '</p>'; 
} 

我只是測試這個代碼,它的工作原理:

$json = file_get_contents('https://www.advantageengagement.com/rest_api/api_test.php/content_data?filter=assignments_list,cs,c010&columns=id_cr,title'); 

$data = json_decode($json, TRUE); 

foreach($data['content_data']['records'] as $record) 
{ 
    echo '<p>' . $record[0] . '<br /> 
    ' . $record[1] . '</p>'; 
} 
+0

謝謝,我已經添加了上面的代碼,但是當我去的頁面,我不'沒有任何東西......只是空白。我還沒有能夠將JSON數據拉回到.php頁面 - 儘管當我把URL放在瀏覽器中時,我可以很好地返回數據......我覺得我錯過了一些東西,我只是不知道什麼:) – user1255154

+0

我已經看到(最近這個星期)從外部資源JSON是腐敗,有額外的空白,有urlencoded實體,等等的外部資源獲得JSON的人。你說你可以在瀏覽器中看到JSON,但這與查看原始json不一樣。如果您允許訪問實際的網址,我可以提供幫助。 –

+0

查看我更新的答案。在底部我實際上測試了代碼,它對我很有用。如果您將我的代碼複製到PHP文件並運行它,您會發現它確實有效。 –

1

下面是如何解析JSON數據到頂部列名的HTML表。

<!doctype html> 
    <html> 
     <head> 
    </head> 
     <body> 
     <?php 
      $json = file_get_contents('https://myurl.com/rest/api.php/content?filter=category,cs,10&columns=id_cr,title'); 
      $data = json_decode($json, TRUE); 
     ?> 
     <table> 
      <!--column names--> 
      <tr> 
       <?php 
       foreach ($data['content_data']['columns'] as $cols) { 
        ?> 
        <td><?= $cols ?></td> 
        <?php 
       } 
       ?> 
      </tr> 

      <!--record rows--> 
      <tr> 
       <?php 
       foreach ($data['content_data']['records'] as $rec) { 
        ?> 
        <td><?= $rec[0] ?></td> 
        <td><?= $rec[1] ?></td> 
        <?php 
       } 
       ?> 
      </tr> 
     </table> 
     </body> 
</html> 
+0

謝謝乍得,我調整了我的PHP頁面中的代碼,但我仍然無法獲得任何數據返回$ file_get_contents url - 網址工作正常,如果我把它放在瀏覽器中,但確實不要在php頁面上拉什麼東西...... – user1255154

+0

如果你可以給出實際的URL,那麼我們可以檢查返回的JSON是否有效並且可以被PHP讀取。謝謝! – chad

+0

以下是URL的結果:{「content_data」:{「columns」:[「id_cr」,「title」],「records」:[[41901,「卓越教育聯邦資源」],[44230,可汗學院「],[145925,」在家教育你的孩子「],[147803,」小學組織和資源「]]} – user1255154

0

當我測試JSON字符串時,它並不是有效的。

   [ 
       145925, 
       "Educating Your Child at Home" 
       ], //this comma has json_decode failing and outputs null 

你可能會檢查你的資源,在哪裏得到JSONString。

爲了訪問JSONObject的值,你可以在PHP中像訪問數組一樣訪問它們。因此,只需使用TRUE作爲$ json_decode的第二個參數。

$json_decode($data, TRUE); 

然後,例如,你可以做到這一點。你可以把它寫多用更少的代碼更乾淨,但是我測試了這一點,它的工作原理:

<!doctype html> 
<html> 
    <head> 
</head> 
    <body> 
     <?php 
     $json = '{ 
      "content_data": { 
       "columns": [ 
       "id", 
       "title" 
       ], 
       "records": [ 
       [ 
       41901, 
       "Resources for Educational Excellence" 
       ], 
       [ 
       44230, 
       "The Khan Academy" 
       ], 
       [ 
       145925, 
       "Educating Your Child at Home" 
       ] 
       ] 
      } 
      }'; 

     $data = json_decode($json, TRUE); 
     var_dump($data); 
     echo "<pre>"; 
      //print out the data a 
     echo "<h2>Data</h2>"; 

     echo "<table>"; 

     echo "<tr>"; 
     //access column data and print out the values 
     echo "<td>". $data['content_data']['columns'][0]."</td>"; 
     echo "<td>". $data['content_data']['columns'][1]."</td>"; 

     echo "</tr>"; 



     for($i = 0; $i < count($data['content_data']['records']);$i++) { 

      echo "<tr id=" .$data['content_data']['records'][$i][0] . ">"; 

      //acces records and print a table with that data 
      echo "<td>" . $data['content_data']['records'][$i][0]."</td>"; 
      echo "<td>" . $data['content_data']['records'][$i][1]."</td>"; 

      echo "</tr>"; 
     } 


     echo "</table>"; 
      //var_dump($data->content_data); 
     echo "</pre>"; 
     exit; 
    ?> 
    </body> 

其結果將是這個

Result of that code

+0

感謝上面的代碼。我一整天都在做這件事,而且我很茫然。當我複製上面的代碼時,我得到了NULL的數據轉儲。我認爲這是因爲我在$ file_get_content ....中的URL中傳遞參數。我不知道... – user1255154