Jump to content

REST call to a URL list, return JSON values into a table


Svarog

Recommended Posts

I am looking for an example, or a link to a working example of this arrangement:
 
You place RESTful call for certain values to a URL which is a JSON list, generated by an API,
then - return those JSON values and display in a generated table. I hope that I have explained
this well enough - the example below explains it better. 
 
So far, I came up with this:
 
<?php
  
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: Example']);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $result = curl_exec($ch);
 
 if (!$result) {
    echo 'Curl error: ' . curl_error($ch);
    die();
 }
 
 $data = json_decode($result, true);
 curl_close($ch);
 
 echo '<table>';
 echo '<tr><th>Part Number</th><th>Description</th><th>Status</th><th>Timestamp</th><th>Datasheet</th></tr>';
 
 $n = 0;
 
 foreach ($data as $key => $jsons) {
    foreach ($jsons as $key => $value) {
       echo '<tr>';
       echo '<td>' . $data[$n]['PartNumber'] . '</td>';
       echo '<td>' . $data[$n]['Description'] . '</td>';
       echo '<td>' . $data[$n]['Status'] . '</td>';
       echo '<td>' . $data[$n]['Timestamp'] . '</td>';
       echo '<td>' . $data[$n]['Datasheet'] . '</td>';
 
       echo '</tr>';
 
       $n++;
    }
 }
 echo '</table>';
 
It pretty much describes what I want to do. At this point, the script brings back raw JSON from the URL
and displays it on the page, and then creates an empty table below raw JSON. It also produces this 
warning: "Warning: Invalid argument supplied for foreach() in eval()... "...
 
- it is obvious, that the engine to choose items for display in the table is missing - it would be nice to get specific values into the appropriate <td>s and this script does not have theengine to do it yet... maybe you could suggest this addition of that code?
 
Thank you for looking at this Post and I appreciate your help in pointing me in the right direction!
 

 

Link to comment
Share on other sites

Thank you for your reply!

 

The API returned and displayed on the page raw JSON in the format:

 

[{"PartID":###,"Category":5,"PartNumber":1267,"Description":blabla.... }]

 

- so, I just need not to display this whole list but display 4 items like "PartNumber", "Description" in the table

that will generate these items out of the whole list. The list of course repeats these items over and over.

Edited by Svarog
Link to comment
Share on other sites


$result = '[{"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"}]';
$data = json_decode($result, true);

echo '<table border="1">';
echo '<tr><th>Part Number</th><th>Description</th><th>Status</th><th>Timestamp</th><th>Datasheet</th></tr>';
foreach ($data as $jsons) {
echo '<tr>';
echo '<td>' . $jsons['PartNumber'] . '</td>';
echo '<td>' . $jsons['Description'] . '</td>';
echo '<td>' . $jsons['Status'] . '</td>';
echo '<td>' . $jsons['Timestamp'] . '</td>';
echo '<td>' . $jsons['Datasheet'] . '</td>';
echo '</tr>';
}
echo '</table>';
Link to comment
Share on other sites

I appreciate your input!

 

OK, I understand that part:

 

"$result = '[{"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"}]';"

 

- but this is hardcoding the result in, isn't it? how do we make, for instance "PartID" values, to be taken

from JSON return list, not hardcoded but dynamicaly generating those values in the table?

Link to comment
Share on other sites


$result = <<<EOR
[
{"PartID":456,"Category":3,"Status":"another status","Timestamp":"99999"},
{"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"},
{"PartID":456,"Category":2,"Description":"some description","Timestamp":"99999"}
]
EOR;

$data = json_decode($result, true);

$header = array();
foreach ($data as $jsons)
$header = array_values(array_unique(array_merge($header,array_keys($jsons))));

echo '<table border="1">';
echo '<tr><th>'.implode('</th><th>',$header).'</th></tr>';
foreach ($data as $jsons) {
echo '<tr>';
$row = array();
foreach ($header as $h)
$row[$h] = (isset($jsons[$h])) ? $jsons[$h] : '';
echo '<td>'.implode('</td><td>',$row).'</td>';
echo '</tr>';
}
echo '</table>';
Link to comment
Share on other sites

OK - the table was generated with your code - my appreciation! no errors, or anything... but - there is the whole list of 

raw JSON from the API that is displayed above the table... I wanted to see if this could be added/changed:

 

1. this JSON to not appear there but rather be used as the source for variables that we need to get;

2. the table to have those "Part Number", etc. areas to be generated from the JSON list, and not hardcoded....  

(like appear inbetween EOR thingies to be inserted into the table, but not handcoded in like in the example that 

works now, but from the JSON list)

 

Here is what I have now (minus the actual URL) and it works - a generated table below a bunch of raw JSON:

 

<?php

  
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: Example']);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $result = curl_exec($ch);
 
 if (!$result) {
    echo 'Curl error: ' . curl_error($ch);
    die();
 }
 
 $result = <<<EOR
[
  {"PartID":456,"Category":3,"Status":"another status","Timestamp":"99999"},
  {"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"},
  {"PartID":456,"Category":2,"Description":"some description","Timestamp":"99999"}
]
EOR;
 
$data = json_decode($result, true);
 
$header = array();
foreach ($data as $jsons)
  $header = array_values(array_unique(array_merge($header,array_keys($jsons))));
 
echo '<table border="1">';
echo '<tr><th>'.implode('</th><th>',$header).'</th></tr>';
foreach ($data as $jsons) {
  echo '<tr>';
  $row = array();
  foreach ($header as $h)
    $row[$h] = (isset($jsons[$h])) ? $jsons[$h] : '';
  echo '<td>'.implode('</td><td>',$row).'</td>';
  echo '</tr>';
}
echo '</table>';
?>
Link to comment
Share on other sites

Hi - I appreciate your help on this greatly! I think, with your help - we are almost there!!

Removing your hardcoded example.... can you stay with me for a few til it's working? thanks~!

 

 

removed

 

$result = <<<EOR
[
  {"PartID":456,"Category":3,"Status":"another status","Timestamp":"99999"},
  {"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"},
  {"PartID":456,"Category":2,"Description":"some description","Timestamp":"99999"}
]
EOR;
Edited by Svarog
Link to comment
Share on other sites

Here is what is left after your hardcoded example was removed - it has an actual url:

 

<?php
  
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: Example']);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $result = curl_exec($ch);
 
 if (!$result) {
    echo 'Curl error: ' . curl_error($ch);
    die();
 }
 
$data = json_decode($result, true);
 
$header = array();
foreach ($data as $jsons)
  $header = array_values(array_unique(array_merge($header,array_keys($jsons))));
 
echo '<table border="1">';
echo '<tr><th>'.implode('</th><th>',$header).'</th></tr>';
foreach ($data as $jsons) {
  echo '<tr>';
  $row = array();
  foreach ($header as $h)
    $row[$h] = (isset($jsons[$h])) ? $jsons[$h] : '';
  echo '<td>'.implode('</td><td>',$row).'</td>';
  echo '</tr>';
}
echo '</table>';
?>
 
 
 
now after saving - the table stopped working and there's these warnings:
 
  • Warning: Invalid argument supplied for foreach() in eval() (line 19 of C:\xampp\apps\drupal\htdocs\modules\php\php.module(80) : eval()'d code).
  • Warning: Invalid argument supplied for foreach() in eval() (line 24 of C:\xampp\apps\drupal\htdocs\modules\php\php.module(80) : eval()'d code).
  • Warning: Invalid argument supplied for foreach() in eval() (line 19 of C:\xampp\apps\drupal\htdocs\modules\php\php.module(80) : eval()'d code).
  • Warning: Invalid argument supplied for foreach() in eval() (line 24 of C:\xampp\apps\drupal\htdocs\modules\php\php.module(80) : eval()'d code). 

 

Edited by Svarog
Link to comment
Share on other sites

Okay so actually, I assumed I couldn't do the curl myself because I needed an actual value for "X-Auth-Token: Example". Turns out I don't.

 

I'm kind of curious how your script is even working.

 

First off, this line is bad syntax, there shouldn't be braces around that 3rd arg like that.

curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: Example']);

 

2nd, you aren't setting curl to put the results in $result. by default it outputs the results to the screen, so you should already be seeing a dump of the results on the page.. in order to have it put into $result, you need to have this:

curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

 

3rd, it looks like it's returning a lot of data and timing out before 10s limit you set on it, which is resulting in malformed json string. IOW you need to up that timeout limit to handle the full results.

 

4th, you did not accurately present the format. It looks like from the data that is returned, each row doesn't just have a simple "header":"value" format. Some of the headers themselves have an array of results. I'm talking about the "Specs" column specifically. So how exactly do you want that to be presented? If you want me to continue helping, you need to make a better effort in explaining exactly what you want.

Link to comment
Share on other sites

are you kidding me? Seriously?? That's what I showed you in my first post.. all you had to do was just echo out the ones you wanted.

 

But then you said that wasn't right, because:

 

- but this is hardcoding the result in, isn't it? how do we make, for instance "PartID" values, to be taken

from JSON return list, not hardcoded but dynamicaly generating those values in the table?

Which I took to mean that the columns needed to be dynamically generated based on the results. If you wanted to only echo out those for columns, why the hell did you even mention "PartID"?

 

WHY ARE YOU WASTING MY TIME.

 

$result = '[{"PartID":123,"Category":5,"PartNumber":1267,"Description":"blabla....","Status":"some status","Timestamp":"123456789","Datasheet":"some value"}]';
$data = json_decode($result, true);
 
echo '<table border="1">';
echo '<tr><th>Description</th><th>Status</th><th>Timestamp</th><th>Datasheet</th></tr>';
foreach ($data as $jsons) {
  echo '<tr>';
    echo '<td>' . $jsons['Description'] . '</td>';
    echo '<td>' . $jsons['Status'] . '</td>';
    echo '<td>' . $jsons['Timestamp'] . '</td>';
    echo '<td>' . $jsons['Datasheet'] . '</td>';
  echo '</tr>';
}
echo '</table>';
Link to comment
Share on other sites

Right.. you said you wanted that, and that's what I initially gave you. Then you said it wasn't right, and went on to explain other columns you didn't mention before, and how it should be dynamic. So I gave you that. Now you are saying no, that wasn't what you wanted, that you just wanted 4 columns. So why did you tell me my first solution was wrong then?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.