I'll start with the most compelling "argument" to why you should not send output before you set a location header (or any other header for that matter): it's impossible => you can't do it.
What do you mean by "posted your stuff"?
The header has to be set, not just before the body tag, it has to appear before ANY output. That is really any kind of output, no matter what it is. This will not work
<?php
header('location...');
since there is a whitespace before the php tag. Whitespace is output.
A HTTP request or response always contains http message header. The header is possibly followed by a blank line and the http message body (a HTTP HEAD request for example will only get a response containing http header).
Do note that what is called http message body has nothing to do with an html body element. When you send an html document, first comes the http header, and then the http body, and the body contains the entire html document: including html, head and body elements.
So as soon as you tell the webserver it's time to send any kind of output the web server will immediately send http headers, which might look like this
response headers from a get request to example.com wrote:
HTTP/1.1 200 OK
Server: Apache/2.2.3 (CentOS)
Last-Modified: Wed, 09 Feb 2011 17:13:15 GMT
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Connection: Keep-Alive
Date: Wed, 22 Jun 2011 09:40:10 GMT
Age: 1
Content-Length: 2945
This means that if you wish to redirect the browser somewhere else using a location header, you have to do this instead of displaying any output. You do not do both.
And looking at your code...
<body>
<?php
if ($response == "true"){
$xml = new SimpleXMLElement($leadResult);
$url = $xml->Redirect;
header("Location: $url"); // if i get a positive response i want to redirect
}
What is the reason to display a body element before you (try to) set location header? The logic performed to decide that you should redirect doesn't check if a body element has been sent to the browser since this has nothing to do with why you redirect. Also, the body element isn't visible to the user, so they won't know it's there. In short, your code doesn't need to send the body element to make its decision, and the user will not notice anything at all, which means there is no reason to send anything before you want to do your redirect.