1

My case as follows: I have a route on server X that returns files to be downloaded by the client's browser (with all headers + filename set).

I want this request to go through a php server, so server X is never exposed to the end user.

Basically what i want to do is to pipe the original request (which was already tested and working as intended) through my php server, retaining all of the original headers.

For some reason I'm having a hard time achieving this, so suggestions could be great.

Thanks in advance!

For clarification:

Lets say we go to the browser and type this url: http://serverX/downloadFile

The result is: Docx file with the name "myfile.docx" downloads.

Now what i want is to pipe this request through my php server, so when i type the url:

The result will be: Docx file with the name "myfile.docx" downloads.

Code wise, i would expect it to look something like that:

Echo Pipe_request($url);

Thanks!

user69153
  • 62
  • 7
  • Hey, ive tried the location header, a bit with curl and the file_get_contents method. My issue is that it doesnt retain the piped request headers. – user69153 Apr 18 '17 at 13:42

2 Answers2

1

Does this work: echo file_get_contents($url); ?

logic-unit
  • 4,195
  • 12
  • 47
  • 72
  • Hey, the thing is it doesnt retain all of the original headers by default, and i'll have to put them again on the php response, which is not ideal as i'll have to edit both sources on every change – user69153 Apr 18 '17 at 13:38
  • Use `$http_response_header` array to set the header? http://php.net/manual/en/reserved.variables.httpresponseheader.php – logic-unit Apr 18 '17 at 13:40
  • But i dont want to set the headers manually, it has been already set on server X which is tested and working. Cant i somehow just pipe the request as is? – user69153 Apr 18 '17 at 13:41
  • So you don't want the original headers? I think you need to update your answer with your current code - I'm not really following what you're trying to do. – logic-unit Apr 18 '17 at 13:43
  • I don't want to have to set them again on my php script. I want them to act exactly as the request from server X do. I'll add the code asap – user69153 Apr 18 '17 at 13:45
  • Not sure if you can do this with PHP. You can create a proxy with your application by fetching the file and headers, then resending those to the client, but obliquely piping from source to client isn't possible as far as I know. – logic-unit Apr 18 '17 at 13:50
  • Hey, i added some information to my question for clarification. Thanks for the help so far! – user69153 Apr 18 '17 at 13:51
  • Thanks for the update. You'll need to fetch and resend the file and headers - I don't think PHP can do what you're looking for. – logic-unit Apr 18 '17 at 13:53
  • 1
    Alright then, I was hoping theres an easy way for doing it, but i'll try implementing it myself. Thank you! – user69153 Apr 18 '17 at 13:55
1

You'd probably want to request given file from the original server, retrieve all send headers (by using something like cURL), copy those and send them to your user.

Take a look at this post, it might help you.

Best of luck!

Community
  • 1
  • 1
Thoby
  • 316
  • 1
  • 6