0

I am using the insert PHP plugin for wordpress to get API data into my pages. so I would replace "" on the last line with "[/insert_php]".

my code

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

returns

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

I have searched, and maybe I am not using the correct terms, and cannot find my solution or answer. I feel like it should be a lot easier than it is. I feel I should be able to take the array from the body and give each its own variable then use the variables to build a table with PHP. Where am I going wrong? Should I first store this in a php file on my server then create a table, then use the insert php function to build the table that way?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ka5050ro
  • 3
  • 4

2 Answers2

0

I've checked the code and it is doing what you programmed it do. Please check you're using correct API URL and once you're using the correct API URL use json_decode function to decode json data returned API and add once you've correct data converted into array, you can create table.

For example:

<?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: This code is only an example, please adjust it your data.

LIMEXS
  • 181
  • 4
  • I guess that's the question I didn't know how to ask was the json decode part. This is the info from the site - Simple, RESTful API - GET and POST supported - Fast, free, unlimited use - Add JSON to any API request by adding &json=true so my question should have been how to put json info into a table? – ka5050ro Jan 04 '16 at 06:15
  • @ka5050ro Alright, it is simple but answer depends on the contents of returned data and what data you need to show table. Once you've decoded data, it is simple PHP array and you can iterate it to create HTML table. I'm modifying table to write generic answer based on scenario and information available. – LIMEXS Jan 04 '16 at 06:21
0

I have tried your code and its working fine for me in local mechine i think you just need to put json_decode for get proper format.

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

Getting this type of output.

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

Please try above code and let me know.

Jalpa
  • 697
  • 3
  • 13
  • @ka5050ro some thing wrong with your code. i am getting array like i put in my answer. – Jalpa Jan 04 '16 at 06:30
  • maybe I am missing a plugin – ka5050ro Jan 04 '16 at 06:32
  • @ka5050ro where did you write your code in project ? – Jalpa Jan 04 '16 at 06:33
  • its in a page using the edit page function within wordpress – ka5050ro Jan 04 '16 at 06:34
  • with the output you show I should be able to use this code http://stackoverflow.com/questions/26213049/how-can-i-parse-json-into-a-html-table-using-php – ka5050ro Jan 04 '16 at 06:36
  • I still need help, but I'm gonna go to bed now, i got work the next 2 days then I will be able to tackle this. I'm not sure why I cannot get the same output you are getting, maybe some sleep will help, but you gave the correct answer there is just something wrong server side – ka5050ro Jan 04 '16 at 07:02
  • @ka5050ro ok fine, if need anything else message me. – Jalpa Jan 04 '16 at 07:15
  • where were you putting this code in? Did you use a seperate .php file? – ka5050ro Jan 07 '16 at 23:16
  • @ka5050ro yes, i put it in template file. which plugin are you using for write php code in editor ? – Jalpa Jan 08 '16 at 04:49
  • sorry I did not see this reply. I am using the insert PHP plugin to use the api in the editor., and I figured out what you did and where you put the code to make it come out like you showed me. My question now is, can I assign the miningsha-256 as variable? or do I store it in a variable? is there a way to make it a global variable once it is called? so I can for instance display it on a user page by calling print_r($variable) and have it show Miningsha-256 => .00205051 – ka5050ro Mar 01 '16 at 04:13
  • @ka5050ro you can use like this global $wp; $wp->miningsha-256 = "00205051"; – Jalpa Mar 01 '16 at 05:47
  • $wp; $wp->miningsha-256 = will this equal what I get form the api? Sorry for my questions I am just a bit confused on how the variables are stored once we capture the information form the API – ka5050ro Mar 01 '16 at 12:27