Svarog Posted November 3, 2013 Share Posted November 3, 2013 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 $url = 'http://domain.com/1.0/parts/parts?categoryGroupID=1'; $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! Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/ Share on other sites More sharing options...
requinix Posted November 3, 2013 Share Posted November 3, 2013 What is the JSON the API returned? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456791 Share on other sites More sharing options...
Svarog Posted November 3, 2013 Author Share Posted November 3, 2013 (edited) 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 November 3, 2013 by Svarog Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456795 Share on other sites More sharing options...
.josh Posted November 3, 2013 Share Posted November 3, 2013 $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>'; Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456798 Share on other sites More sharing options...
Svarog Posted November 3, 2013 Author Share Posted November 3, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456801 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 $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>'; Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456804 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 My apologies - what does EOR represent here? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456806 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 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 $url = 'http://api.domain.com/1.0/parts/parts?categoryGroupID=1'; $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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456807 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 so like, I was using hardcoded values as an example since I don't have access to make the curl request like you do. Your code generates $result from the curl request, so just remove my $result =<<<EOR..EOR; example data. Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456809 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 (edited) 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 November 4, 2013 by Svarog Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456811 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 (edited) Here is what is left after your hardcoded example was removed - it has an actual url: <?php $url = 'http://api.macomtech.smrtsrc.com/1.0/parts/parts?categoryGroupID=1'; $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 November 4, 2013 by Svarog Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456812 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 okay, well then that means your curl request isn't returning expected results. After $data = json_decode($result, true); echo "<pre>"; print_r($data); exit(); What does that output? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456813 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 I changed $data = json_decode($result, true); to $data = json_decode($result, true); echo "<pre>"; print_r($data); exit(); that output brought a full-blown JSON list (the whole page) Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456814 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 You saw for yourself it worked with the example json. So either what is returned isn't really json or it's somehow malformed or not in the same format as the example you showed or something. Anyways, how am i supposed to figure out what's wrong when I can't see it? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456815 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 $url = 'http://api.macomtech.smrtsrc.com/1.0/parts/parts?categoryGroupID=1'; sorry, I do not know what else can I show you... Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456816 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456817 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 - thank you for your help. - I just wanted those 4 values to appear in the table 'Description' 'Status' 'Timestamp' 'Datasheet', not the whole list... Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456818 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456821 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 I did not mean to upset you so much, I've explained in my Post 1 what I wanted to accomplish... sorry it turned so not nice... Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456823 Share on other sites More sharing options...
.josh Posted November 4, 2013 Share Posted November 4, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456825 Share on other sites More sharing options...
Svarog Posted November 4, 2013 Author Share Posted November 4, 2013 I am sorry - all my fault..... but I just want to say - this was a misunderstanding of big proportions, and I apologize... Quote Link to comment https://forums.phpfreaks.com/topic/283569-rest-call-to-a-url-list-return-json-values-into-a-table/#findComment-1456826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.