2016-01-04 35 views
0

我正在使用插入PHP插件來獲取API數據到我的頁面。 所以我會用「[/ insert_php]」替換最後一行的「」。使用php將API數據放入表中

我的代碼

[insert_php] 
$url = 'https://www.eobot.com/api.aspx?idspeed=285967&json=true'; 

$args = array (
    'method' => 'GET' 
); 

$response = wp_remote_request($url, $args); 

$body = wp_remote_retrieve_body($response); 
print_r($body); 
[/insert_php] 

回報

MiningSHA-256:0.00000000; MiningScrypt:0.00000000; CloudSHA-256:12.72592330; Cloud2SHA-256:0.01894240; CloudScrypt:0.00000000;

我已經搜索過,也許我沒有使用正確的術語,並且找不到我的解決方案或答案。我覺得它應該比現在容易得多。我覺得我應該能夠從身體中獲取數組,然後爲每個變量賦予它自己的變量,然後使用變量來使用PHP構建表。我哪裏錯了?我應該首先將它存儲在我的服務器上的php文件中,然後創建一個表,然後使用插入php函數來構建表?

+0

您正試圖訪問「Private API」和「Private API」所需的帳戶。檢查此鏈接的更多信息:https://www.eobot.com/developers –

+0

這是我的私人帳戶,因爲你可以看到它有我的用戶ID,因爲它應該 – ka5050ro

回答

0

我試過你的代碼,它在我的本地機器上正常工作,我認爲你只需要把json_decode取得適當的格式即可。

[insert_php] 
$url = 'https://www.eobot.com/api.aspx?idspeed=285967&json=true'; 

$args = array (
    'method' => 'GET' 
); 

$response = wp_remote_request($url, $args); 

$body = wp_remote_retrieve_body($response); 
print_r($body); 
echo "<pre>"; 
$data = json_decode($body); 
print_r($data); 
[/insert_php] 

獲取此類型的輸出。

stdClass Object 
(
    [MiningSHA-256] => 0.00000000 
    [MiningScrypt] => 0.00000000 
    [CloudSHA-256] => 12.72656020 
    [Cloud2SHA-256] => 0.01894240 
    [CloudScrypt] => 0.00000000 
) 

請嘗試上面的代碼,並讓我知道。

+0

http://depositbox.cc/557-2/這是我得到的新代碼 – ka5050ro

+0

@ ka5050ro你的代碼有些問題。我越來越像我放在我的答案陣列。 – Jalpa

+0

也許我缺少一個插件 – ka5050ro

0

我已經檢查了代碼,它正在做你所編程序的事情。請檢查您是否使用了正確的API網址,並且一旦您使用正確的API網址,使用json_decode函數來解碼json數據返回的API並添加一次將正確的數據轉換爲數組後,您可以創建表。

例如:

<?php 
$body = wp_remote_retrieve_body($response); 
$table_data = json_decode($body); 
$html = array(); 
if(!empty($table_data)){ 
    $html[] = '<table>'; 
    foreach($table_data as $rows){ 
    $html[] = '<tr>'; 
    foreach($row as $column){ 
     $html[] = '<td>'. $column . '</td>'; 
    } 
    $html[] = '</tr>'; 
    } 
    $html ='</table>'; 
} 
$html = implode("\n", $html); 
echo $html; 
?> 

PS:此代碼只是一個例子,請調整它的數據。

+0

我想這是我不知道如何問的問題是json解碼部分。這是從網站的信息 - 簡單,RESTful API - 支持GET和POST - 快速,免費,無限制使用 - 通過添加&json = true添加JSON到任何API請求,所以我的問題應該是如何把json信息成一張桌子? – ka5050ro

+0

@ ka5050ro好的,它很簡單,但答案取決於返回的數據的內容以及需要顯示錶格的數據。一旦你解碼了數據,它就是簡單的PHP數組,你可以迭代它來創建HTML表。我正在修改表來根據可用的場景和信息編寫通用答案。 – LIMEXS

相關問題