Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将API数据放入表中_Php_Wordpress_Http Get - Fatal编程技术网

使用php将API数据放入表中

使用php将API数据放入表中,php,wordpress,http-get,Php,Wordpress,Http Get,我正在使用wordpress的insert 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)

我正在使用wordpress的insert 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函数以这种方式构建表吗?

我已经检查了代码,它正在执行您编程的操作。请检查您是否使用了正确的API URL,一旦您使用了正确的API URL,请使用
json\u 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;
?>


PS:此代码只是一个示例,请调整您的数据。

我已经尝试了您的代码,它在本地机器中运行良好,我认为您只需要将json_解码以获得正确的格式

[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]

请尝试上述代码并通知我。

您正在尝试访问
私人API
私人API
所需帐户。查看此链接了解更多信息:这是我的私人帐户,因为你可以看到它有我的用户ID,因为它应该有我的用户ID。我猜这是我不知道如何问的问题,是json解码部分。这是来自网站的信息-简单、RESTful API-获取和发布支持-快速、免费、无限制使用-通过添加&JSON=true向任何API请求添加JSON,所以我的问题应该是如何将JSON信息放入表中?@ka5050ro好的,这很简单,但答案取决于返回数据的内容以及需要显示表的数据。一旦您解码了数据,它就是简单的PHP数组,您可以迭代它来创建HTML表。我正在修改表格,以便根据场景和可用信息编写通用答案。@ka5050ro您的代码有问题。我得到了一个数组,就像我在回答中说的那样。也许我遗漏了一个数组plugin@ka5050ro您在project中的何处编写代码?它位于使用WordPress中的“编辑页面”功能的页面中,您显示的输出表明我应该能够使用此代码
stdClass Object
(
    [MiningSHA-256] => 0.00000000
    [MiningScrypt] => 0.00000000
    [CloudSHA-256] => 12.72656020
    [Cloud2SHA-256] => 0.01894240
    [CloudScrypt] => 0.00000000
)