1

I am integrating another companies services with ours and they want to send an xml post request to us and get an 200 response with xml, but then I also need to forward the request.

Their user gets sent to our site when they click on a link on their page, which sends an xml post request to us. We are supposed to send an xml response back, and then forward the user to our home page.

I have figured out one or the other, but not both by setting headers.

For the 200 response:

header("Content-type: text/xml");
header("HTTP/1.1 200 OK");
header("Status: 200");

echo 'my-xml';
exit;

For the forward:

header("Location: http://www.example.com/");
exit;

But I can't figure out how to do both?

Also, this is being done in Wordpress by intercepting the pre_get_posts filter, which works fine.

  • why do you want to do that? you could send a `303 See Other`. – kelunik Sep 23 '13 at 20:21
  • you can't. `Location` requires a 301 response code, which would replace the `200`. You cannot do two different responses like they want. – Marc B Sep 23 '13 at 20:22
  • Headers must precede output. Here you either have to redirect and output, which makes no sense. Or, you have to output and then redirect, which will not work. – Boaz Sep 23 '13 at 20:23
  • I can think of an ugly `header('Refresh:0;url=http://www.example.com/');`, but I doubt it will work, 'cause I have a hard time believing there is [a client doing a non-Ajax request sending that xml-post](http://stackoverflow.com/questions/10169135/browser-ignoring-header-refresh-from-ajax-response). – Wrikken Sep 23 '13 at 20:33

1 Answers1

3

You can't do this purely in HTTP headers. Executing header("Location: http://www.example.com/"); causes PHP to do an implicit Status: 302 response, and the output before the header() call will also trigger a Warning.

Also, since you're sending XML to the browser, and not HTML, there is no other mechanism that can perform the redirect. Either you're misunderstanding the requirement you've been given, or the API you're integrating with is asking you to do something impossible.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • You can also send `301` and `303`, maybe more while redirecting. – kelunik Sep 23 '13 at 20:29
  • is there a way to forward without setting header location? Does HttpResponse::send() allow actions afterward if I replaced the original header response with that? –  Sep 23 '13 at 20:35