user924069
user924069

Reputation:

PHP - How do I send a 200 code response to a request and also forward it?

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.

Upvotes: 1

Views: 1147

Answers (1)

Sammitch
Sammitch

Reputation: 32232

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.

Upvotes: 3

Related Questions