speckytwat2 Posted January 28, 2021 Share Posted January 28, 2021 Hi, I have a connection set up to an API using a PHP script - the API sends back data in JSON format, and if I capture it and echo it, it displays in extremely unfriendly format on the screen. I've tried to find a way to convert this data into readable format, ideally in a HTML table, but although there are articles on converting manually inputted JSON data into a HTML table using Javascript, I can't find a way to do it from a PHP variable. This is what I have so far: if ($_POST['getcompany']) { $companyname = $_POST['_Name']; $ch = curl_init(); $data_array2 = array( 'token' => $token ); $make_call2 = json_encode($data_array2); //echo 'Token is '.$token; $token2 = substr($token, 14); $token3 = substr($token2, 0, -3); //echo 'Token 2 is '.$token3; //echo 'Company Name is '.$companyname; //curl_setopt($ch, CURLOPT_GET, 1); //curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call2); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: '.$token3.'')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://connectionurl?countries=GB&name='.$companyname); //Execute the request $result = curl_exec($ch); echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format So I need a way to get the data held in $result, into a HTML table. Any idea how I can do this? Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/ Share on other sites More sharing options...
gw1500se Posted January 28, 2021 Share Posted January 28, 2021 Check out the json_encode documentation and the JSON_PRETTY_PRINT option. Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584090 Share on other sites More sharing options...
speckytwat2 Posted January 28, 2021 Author Share Posted January 28, 2021 (edited) Thanks, I already used json_encode above, should I be using it on $result as well? i.e $resultencoded = json_encode($result); I tried that but it just seems to add slashes in front of the quotation marks? Also tried this: <pre><?php echo json_encode($result JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); ?></pre> and <pre><?php echo json_encode($result JSON_PRETTY_PRINT); ?></pre> But it still gives an unfriendly output, putting lots of instances of \n in place? Edited January 28, 2021 by speckytwat2 Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584091 Share on other sites More sharing options...
Barand Posted January 28, 2021 Share Posted January 28, 2021 (edited) 22 minutes ago, speckytwat2 said: echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format Sounds like you need json_decode() Edited January 28, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584092 Share on other sites More sharing options...
speckytwat2 Posted January 28, 2021 Author Share Posted January 28, 2021 (edited) 6 minutes ago, Barand said: Sounds like you need json_decode() Thanks... unfortunately I tried that: $decoded = json_decode($result, true); and $decoded = json_decode($result); The 1st just outputted "Array" and the 2nd threw an error: "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in..." then "Stack trace: #0 {main} thrown in..." I also tried this: $decoded = json_decode($result, JSON_PRETTY_PRINT); But that still outputs the same data, albeit with Array => instances added. Just need a way to get this somehow exported to a HTML table? Edited January 28, 2021 by speckytwat2 Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584093 Share on other sites More sharing options...
gw1500se Posted January 29, 2021 Share Posted January 29, 2021 Sorry, I didn't realize that was what you wanted to do. Someone has already done that for you on github. Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584114 Share on other sites More sharing options...
speckytwat2 Posted February 1, 2021 Author Share Posted February 1, 2021 On 1/29/2021 at 2:56 PM, gw1500se said: Sorry, I didn't realize that was what you wanted to do. Someone has already done that for you on github. Thanks, looking at that though the only problem is the JSON data being pulled from the API is going to be different each time, so I can't see how it would work- how would I throw all the raw JSON data into the objectArray variable? I can put it into a PHP variable and in fact tried creating a JSON file from that, so it would have whatever the queried data was on that occasion: $fp = fopen('result.json', 'w'); fwrite($fp, $result); //Where $result is the data pulled through from the API But I don't think the example script can read a specially created JSON file? Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584187 Share on other sites More sharing options...
speckytwat2 Posted February 2, 2021 Author Share Posted February 2, 2021 Anyone have any ideas how this can be achieved? Quote Link to comment https://forums.phpfreaks.com/topic/312066-how-to-convert-json-data-inside-php-variable-into-readable-format-in-table/#findComment-1584195 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.