1

I am working with an API for a payment gateway that does a callback request. When the callback request is made, the gateway expects me to respond with "OK". Nothing more or less. And that doesn't mean html rendered response. Just a callback file with those 2 letters. Note that doesn't mean it wants HTTP Status Code 200/OK... it wants actual data (not headers) for the word "OK".

So this won't work:

<html><body>OK</body></html>

This will work:

<?php echo "OK"; ?>

however, after I send back OK, I need to do some stuff on the server side and then redirect the browser page to another page. But when I try to do:

<?php
echo "OK";
header('Location: http://www.store.com/success.php');
exit;
?>

The gateway ignores the echo "OK" and instead reads the html off of the success.php page that I redirect to.

So how can I send back just the OK but continue doing things on my side?

Thanks

Dss
  • 2,162
  • 1
  • 24
  • 27
  • What happens if you send the `Location:` header first? – WWW Jun 18 '12 at 16:27
  • Afer you send something in the body you can not change/add a HTTP header. – powtac Jun 18 '12 at 16:27
  • Same result. It still gets the html of the redirect page – Dss Jun 18 '12 at 16:28
  • It seams you went wrong way, you should talk with gateway by services. – Mohammad Ali Akbari Jun 18 '12 at 16:29
  • Is the redirect before the html tags? – thegrede Jun 18 '12 at 16:29
  • Aren't you redirecting the API server rather than the user's browser there? I think the solution will be considerably more complicated than this, exactly how you do it depends on *exactly* what you are trying to do. I'm guess you will need to have the `OK` script log some data to a database, and have the browser poll the database for updates until this happens. – DaveRandom Jun 18 '12 at 16:31
  • Why would you redirect a callback? Just handle the callback and echo OK as instructed. – Repox Jun 18 '12 at 16:34
  • 1
    If an API is calling your page, what is the purpose of redirecting the browser? It sounds like they follow the redirect but still expect the `OK` response. How much stuff do you need to do on your side (i.e. 1 second of processing of 5 minutes)? If its short, just do it and then output OK, otherwise you will need to spawn off another process in the background. If you use PHP-FPM, then you get the function `fastcgi_finish_request()` which lets you terminate the request and continue processing. – drew010 Jun 18 '12 at 16:35

4 Answers4

1

You can't send content then redirect. The redirect header setting must be done alone.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • Thanks for the answer.. I had feared this.. ok.. I will try to talk to the gateway and ask them what they expect people should be doing in this case. Thanks – Dss Jun 18 '12 at 16:30
0

You could try using flush(); to force php to write out the OK.

blearn
  • 1,198
  • 10
  • 17
  • @powtac Look to this for flush(): http://stackoverflow.com/questions/11072717/stop-looping-and-output-and-then-continue-php/11072848#11072848 – HBv6 Jun 18 '12 at 16:32
0

Would it be acceptable to move the redirect into client side either with javascript or meta refresh tag?

AllInOne
  • 1,450
  • 2
  • 14
  • 32
0

Before you send the OK you could call PHP via commandline to make PHP acting like multi threated.

powtac
  • 40,542
  • 28
  • 115
  • 170