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.

3
  • the result array seems okay, maybe what you mean you wanted them to display as json format, since this is api Commented Mar 29, 2017 at 9:41
  • 1
    Save $api->response in an array variable and then you can show it in form of table. Better approach is provided here : stackoverflow.com/questions/4746079/… Commented Mar 29, 2017 at 9:49
  • Better to convert it to JSON format. Then render it to your Table. Commented Mar 29, 2017 at 11:14

2 Answers 2

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";
Sign up to request clarification or add additional context in comments.

Comments

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 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.