I am having problems trying to create a table using json data and displaying it in a WordPress page. I have tested the json import and it does work. I can render the json on the WordPress page but when I try to convert it to a table I am getting no results.
Below is the php code...
<!DOCTYPE HTML>
<html>
<head>
<?php
/*
Template Name: Serve
*/
$url = "https://myapidata";
$username = 'login';
$password = 'passwd';
$headers = ['Authorization' => 'Basic ' . base64_encode( "$username:$password" ), "sslverify" => false];
$response = wp_remote_get($url);
if (is_wp_error($response)) {
echo $response->get_error_message();
} else {
$data = wp_remote_retrieve_body( $response );
$table = '<table>';
$table .= '<tr>';
foreach ($data[0] as $key => $value) {
$table .= '<th>' . $key . '</th>';
}
$table .= '</tr>';
foreach ($data as $row) {
$table .= '<tr>';
foreach ($row as $value) {
$table .= '<td>' . $value . '</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
$roster = $table;
return $table;
}
?>
</head>
<body>
<?php echo $roster;?>
</body>
</html>
source https://stackoverflow.com/questions/77601429/page-content-not-printed-after-return
No comments:
Post a Comment