0

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Duck
  • 34,902
  • 47
  • 248
  • 470
  • 2
    Please construct a test-case. – Oliver Charlesworth Jan 29 '13 at 14:21
  • can you be more specific? – Duck Jan 29 '13 at 14:23
  • The second line is the HTTP response: version 1.1, error code 200 ("the request was fulfilled") - the shorthand for which is "OK" – Floris Jan 29 '13 at 14:23
  • ok, I know. I am asking if this is a normal string to come out when all I am doing is creating a plain txt from the server. I am also asking if this is the correct way to print a clean txt. – Duck Jan 29 '13 at 14:24
  • I understand the question now. You want the response to be a line of text, not a complete html file which would be rendered by the browser as a line of text inside a well formed html document. – Floris Jan 29 '13 at 14:27
  • @Floris - yes. Sorry if my question was not clear enough. Sometimes my english tricks me... I have to think in another language and convert it to english and sometimes things go wild... :) – Duck Jan 29 '13 at 14:29
  • Can't imagine it makes a difference, but what happens if you use `header("Content-Type: text/plain");` as your header, rather than the single-quoted version? – Floris Jan 29 '13 at 14:30
  • Are you sure isn't the php page outputting the above html code? – Davide Berra Jan 29 '13 at 14:31
  • @Floris - unbelievable but changing it to double quotes changed the results completely. Now I am getting some garbage characters too... :( – Duck Jan 29 '13 at 14:33
  • How did you "debug the result"? What browser did you use to "make direct request"? – Alepac Jan 29 '13 at 14:40
  • Which browser are you using? Chrome is acutally adding and correcting HTML for the output. – ConcurrentHashMap Jan 29 '13 at 14:40

3 Answers3

3

I have never seen an extra line being added below the output by PHP. I would suggest you check:

  • I assume the PHP file is run on a web server. This means when debugging, you are probably fetching the result with HTTP using some tool. I think this tool is what adds the additional line. Use the command-line tool curl to get a clean output. That will also show exactly what the server sends. The added HTML is probably added by Chrome for display purposes.

  • Your code for something that could generate it (e.g. by adding an exit/die statement right after the echo that generates the desired output). Also add an additional echo there temporarily to make sure you are not executing an outdated file that contains old debug output.

Since the unwanted line doesn't appear in the (HTML-wrapped) response displayed by your browser, I am pretty sure your HTTP fetching tool is adding it.

Jan Schejbal
  • 4,000
  • 19
  • 40
1

Encoding is missing in your header:

header('Content-type: text/plain; charset=utf-8');

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.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
Josef Kufner
  • 2,851
  • 22
  • 28
  • Hey @Josef, welcome to SO. Just to let you know, there is a no-swearing policy here - http://meta.stackexchange.com/questions/22232/are-expletives-allowed-on-se-sites/22233#22233. – Dan Blows Jan 29 '13 at 14:43
1

There was a previous article here. The important point it made: the header() comment must be the VERY FIRST THING in the file - no white spaces, no anything.

I just made a test case for myself:

<?php header("Content-Type: text/plain");
echo "hello world\n";
?>

works - it generates "pure text" as output.

If you are interested - that file is at http://www.floris.us/SO/SO1.php

How does it arrive at your browser (when you view source code)?

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122