开发者

View contents of file with Content-disposition: attachment using socket

I want to parse the contents of a dynamic .csv file. However, this code (obviously with duff file names, etc.):

$socket = fsockopen("www.example.com", 443);
fwrite($socket, "GET /dynamicCsv.csv?param=value HTTP/1.1\r\n");
fwrite($socket, "Host: www.example.com\r\n");
fwrite($socket, "Connection: close\r\n");
fwrite($socket, "\r\n");

while(!feof($socket)) echo fgets($socket);

...just downloads the file.

Using openssl at command line I can type the exact same request and get the following response header sent back:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename=dynamicCsv.csv
Pragma: public
Cache-Control: max-age=0
Set-Cookie: SESSIONID=ASDFUHN023UIN0F; Path=/; Secure
Content-Type: text/csv;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 04 Feb 2010 16:53:00 GMT

To avoid having the file download automatically I have tried to chop off the first character using substr, in the hope that maybe the browser sees the response output by fgets as a set of headers of the current document. That didn't work. Pursuing the same idea I also tried to insert a bunch of newlines before opening the socket, which didn't work either.

My two questions are:

  1. Why on earth does the browser think it should download the file?
  2. How 开发者_如何学运维can I stop it?

I should mention that I've only used Chrome so far, but I don't see why it would make a difference.

Many thanks, Andreas


Ok, I actually managed to solve it myself. For reference, instead of removing the question I add this response with the fix.

The problem was that in

fsockopen("www.example.com", 443);

I did not specify to use the ssl protocol. So the fix is to instead type:

fsockopen("ssl://www.example.com", 443);


You could consider using cURL or some other wrapper for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜