0

is there any way to retrieve binary data file send as response for POST request?

I.e. You customize your package in some page, than click Submit and HTTP server resposne for this POST request with ZIP archive.

I know that I can send POST request with cURL, but whats next to do? How to retrieve this file?

Thanks

3
  • 1
    Did you try to file_put_contents() on the response of the curl request? Commented Aug 27, 2013 at 5:23
  • not file_get_contents()? Commented Aug 27, 2013 at 5:48
  • 1
    If you want to save it, it's file_put_contents (putting the output to a file). Commented Aug 27, 2013 at 6:04

2 Answers 2

1

Use cURL to send the POST request (I'm not showing that part of the code, you said you know how to do that). The returned response will be the contents of the ZIP file. You can then save it in a local file, or send it to the client; I show the first option below.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
...
$result = curl_exec($ch);
file_put_contents("foo.zip", $result);

You can also use the CURLOPT_FILE option to write directly to the file instead of using a variable first:

$fh = fopen("foo.zip", "w");
curl_setopt($ch, CURLOPT_FILE, $fh);
close($fh);
Sign up to request clarification or add additional context in comments.

6 Comments

How would you catch this attachment then in the success function of an AJAX call?
@cars10 You wouldn't. This is intended as the response to a normal form submission. The browser will then pop up a file save dialogue.
Ahh, life can be so easy if you (in this case I) read the question properly! ;-))
No no no, it's not what i mean. I know that if MY SERVER wonna serv file to client than yes - this is good answer. But ANOTHER SERVER is sending to MY SERVER file as attachment, and I wonna to MY SERVER download it.
Do you want to download it locally on the server, or send it back to the original client?
|
0

One solution might be to have the server put this Zip archive together and then send the link in the Post response back.

1 Comment

Well, this was assuming, that the POST was used in an AJAX request. So ... forget about it! ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.