1

I'm a beginner in PHP and working on an API project which has to get results from a server I connected via API.

I am getting the results, but I would like to have them well arranged in a simple table.

This is what I am getting:

response

I used the following code:

$api = new SplynxAPI($api_url, $key, $secret);

$locationsApiUrl = "admin/administration/locations";

echo "<pre>";

echo "List locations\n";
$result = $api->api_call_get($locationsApiUrl);

echo "Result: ";

if ($result) {
  echo "Ok!\n";
  print_r($api->response);
} else {
  echo "Fail! Error code: $api->response_code\n";
  print_r($api->response);
}

echo "\n-------------------------------------------------\n";

Kindly assist me on this one.

mortalis
  • 2,060
  • 24
  • 34
willy
  • 17
  • 5
  • the result array seems okay, maybe what you mean you wanted them to display as json format, since this is api – Beginner Mar 29 '17 at 09:41
  • 1
    Save $api->response in an array variable and then you can show it in form of table. Better approach is provided here : http://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array – Saili Jaguste Mar 29 '17 at 09:49
  • Better to convert it to JSON format. Then render it to your Table. – Deepak swain Mar 29 '17 at 11:14

2 Answers2

1

The following code will render your response in a HTML <table>.

$api = new SplynxAPI($api_url, $key, $secret);

$locationsApiUrl = "admin/administration/locations";


$result = $api->api_call_get($locationsApiUrl);

if ($result) {
  echo "<table>";
  foreach($api->response as $row){
    echo sprintf('<tr><td>%s</td><td>%s</td></tr>', $row['id'], $row['name']);
  }
  echo '</table>';
} else {
  echo "Fail! Error code: $api->response_code\n";
  print_r($api->response);
}

echo "\n-------------------------------------------------\n";
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
0

You are directly getting the response "$api->response" as an array. So simply use a loop and populate it in table as you like.

Ex:

foreach($api->response as $apiData) {
 // Your code 
}