Skip to content
Advertisement

Wrapping json results in tag

I am trying to make my own API for my discord bot and i noticed the response looks like this: Showing JSON code in font Times New Roman font probably times new roman because it is not wrapped in a <pre> tag.

But when I DO wrap it in a <pre> tag to make it look like this: Showing JSON Code in font Monospace(font monospace) the contents have the tags in it…

For example these are the contents that are grabbed:

JavaScript

The code:

JavaScript

I also tried this with no avail:

JavaScript

How would I go about getting the json wrapped in the <pre> tag without it affecting how it grabs the content? I feel like I have looked everywhere but can’t find a thing. I know it can be done because an API I use gets wrapped in the <pre> tag and doesn’t have a problem retrieving the json.

Advertisement

Answer

Include this header('Content-Type: application/json'); and make sure you have NO empty spaces above your <?php tag!

That was the problem here, a simple empty space/line above the <?php tag.

So my second code block should’ve worked, had I not left an empty line above the <?php tag at the top of the PHP file. e.g:

Before (output.php):

JavaScript

After (output.php):

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement