当响应是content

编程入门 行业动态 更新时间:2024-10-10 15:25:36
本文介绍了当响应是content-disposition:附件时,似乎不能使用PHP Curl下载CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用PHP Curl从网址下载CSV文件,但Curl响应是网页的HTML,而不是CSV。如何解决此问题?

我已尝试匹配referer和用户代理。

这是firefox中的工作请求:

GET [someURL ] HTTP / 1.1 Host:[someHost] 用户代理:Mozilla / 5.0(Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13)Gecko / 20101203 Firefox / 13 Accept:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8 Accept-Language:en-us,en; q = 0.5 Accept-Encoding:gzip,deflate Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.7 Keep-Alive:115 Connection:keep-alive Referer:[someReferer] Cookie:[某些cookie数据,包括jsession]

这里是响应:

HTTP / 1.1 200 OK Content-Type:text / csv 日期:2011年6月5日星期日00:33:21 GMT 过期日期:2011年6月5日00:34:21 GMT Content-Disposition:attachment; filename = [some file name.csv] Connection:Keep-Alive Last-Modified:Sun,05 Jun 2011 00:33:21 GMT X-Powered-By:Servlet / 2.5 JSP / 2.1 Content-Length:5012

解决方案

我认为您正在寻找的选项是 -J :

从 curl.haxx.se/docs/manpage.html#-J :

-O,--remote-name选项使用服务器指定的Content-Disposition文件名,而不是从URL中提取文件名。

I am trying to download a CSV file from a URL using PHP Curl, but the Curl response is the HTML of the page, and not the CSV. How do I fix this?

I've tried matching the referer and user-agent.

Thanks!

Here is a working request in firefox:

GET [someURL] HTTP/1.1 Host: [someHost] User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Referer: [someReferer] Cookie: [some cookie data, including jsession]

and here's the response:

HTTP/1.1 200 OK Content-Type: text/csv Date: Sun, 05 Jun 2011 00:33:21 GMT Expires: Sun, 05 Jun 2011 00:34:21 GMT Content-Disposition: attachment; filename=[some file name.csv] Connection: Keep-Alive Last-Modified: Sun, 05 Jun 2011 00:33:21 GMT X-Powered-By: Servlet/2.5 JSP/2.1 Content-Length: 5012

解决方案

I think that option that you are looking for is -J:

From curl.haxx.se/docs/manpage.html#-J:

-J, --remote-header-name

(HTTP) This option tells the -O, --remote-name option to use the server-specified Content-Disposition filename instead of extracting a filename from the URL.

更多推荐

当响应是content

本文发布于:2023-10-10 09:31:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!