我正在使用wordpress的插入PHP插件将API数据获取到我的页面中。所以我将最后一行的"替换为"[/insert_php]"。
我的代码
[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文件中,然后创建一个表,然后使用insert php函数以这种方式构建表吗?
我已经尝试过你的代码,它在本地机制中对我来说很好,我认为你只需要放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
)
请尝试上面的代码并告诉我。
我已经检查了代码,它正在做你编程它所做的事情。请检查你使用的是正确的API URL,一旦你使用了正确的API URL,请使用json_decode
函数解码API返回的json数据,并在你将正确的数据转换为数组后添加,你就可以创建表了。
例如:
<?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;
?>
附言:这个代码只是一个例子,请调整你的数据。