I have this php page that gets values from a database. From another script on another server, I make a http request to that page and grab the result. The result must be a clean TXT, something like a phrase for example, or a number, or whatever.
When I debug the results I get I see something like this (suppose the answer I was expecting was +OK:server ready+):
+OK:server ready+
1.1 200 OK
I am getting this extra line.
If I make a direct request using a browser, I see +OK:server ready+ but when I look at the page source of that result page I see this:
<html>
<head>
<style type="text/css"></styles>
</head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">+OK:server ready+</pre>
</body>
</html>
Apparently the browser is adding this, right?
Anyway, the code that generates the line I want is this:
header('Content-Type: text/plain');
echo("+" . $resultFromServer . "+");
is this the correct way to send out a clean text, without any formatting, tag, whatever, from PHP?
thanks.
Encoding is missing in your header:
But it should work fine without it as long as you send only ASCII characters.
Browser should not add any html code, but I’ve seen some PHP frameworks that messed up non-standard output. Check if there is any extra output processing in your application. But if you have only that two lines in your PHP script, it must work.
Btw, echo is not a function and it accepts multiple arguments, so you do not have to concatenate strings with dot operator –
echo 'a', $b, 'c';is much faster.