Making a delayed response from a header

Making a delayed response from a header

I need to receive a POST and generate a file with a “201 Created” response. The problem is that I can not figure out how to delay the header until the new file is created and URI to the file is known.

I know I can delay the coontent with ob_Start and ob_flush but that says it does not work for headers

Any suggestions?

I am a bit confused on why you would want to do this? PHP is SERVER-SIDE only and runs BEFORE the headers are sent out to the browser. Therefore, just do your processing and continue to send the live headers when done.

Perhaps you need to explain what you are attempting to do.

Let me try to explain

The Purpose is a “201 Created”" header
That same header should have a URL Like

http://thisserver.com/yourfile.jpg

I can not provide the link to the file (in the header) till it is actually available. This is to conform to the eSCL protocol and keep eSCL clients like MacOS and Mopria Scan for Android happy. Also because the file has not yet been generated nor has the UUID , which is tied to date/time to guarantee uniqueness.

So what needs to happen is the client makes a POST requesting a page be scanned from the document feeder in color at 300 dpi and saved as jpg

I need to NOT provide the header until the link is actually VALID (this is also to be able to create a valid unique link to this file)

So then I create a filename like Scan202107081631.jpg and the scan is posted to UUID like this

http://thisserver.com/<UUID>/Scan202107081631.jpg

I guess the point is NOT that you understand WHY, rather that this is how it MUST GO.

Well, if you want to set that header, just do it. Something like this should work.

header("HTTP/1.0 201 Created");
header("HTTP/1.1 201 Created");

Or if PHP version 5.4 or greater,

http_response_code(201);

Not sure if that is what you are asking for. You say you don’t want to post a header until the file is created, but, PHP is server side and should not send any headers out until it is complete and sends the first character to the browser. But, you probably can send it this way, run the scan and then continue with the page. But, I think that would work the same if you just run the scan before sending anything and then just send the page with the new image in it. I do that with complicated reports and website scrapes that take many minutes to process and never have any issue just running the process before any characters are sent…

Does it have to be a synchronous operation?

There are a few ways you can do this, but it largely depends on how you want to do it and the length of time the operations take.

For instance, you can make a post and not return anything until the operation is done; hence do the 201 after it was done in a single call. But. from a scalability level, I would want this done in a queue rather than synchronous. In that model you can update the client when the operation is completed. You can also create a reference ID and when the actual filename comes back, replace it through the routing aspects.

Sponsor our Newsletter | Privacy Policy | Terms of Service