如何生成与Firefox生成相同响应头的cURL请求

时间:2016-08-19 20:53:24

标签: php http firefox curl http-headers

当我浏览到使用Firefox的页面并单击下载链接时,在网络检查器中检查请求时会显示以下标题:

Connection: keep-alive
Content-Disposition: attachment; filename="example_file.mp3"
Content-Length: 35181829
Content-Transfer-Encoding: binary
Content-Type: audio/mpeg
Date: Fri, 19 Aug 2016 18:19:02 GMT
Keep-Alive: timeout=60
Server: nginx
X-Powered-By: PHP/5.4.45

但是,当我使用cURL访问同一地址时,我明白了:

Connection: keep-alive
Content-Length: 1918
Content-Type: text/html; charset=UTF-8
Date: Fri, 19 Aug 2016 20:46:23 GMT
Keep-Alive: timeout=60
Server: nginx
X-Powered-By: PHP/5.4.45

如何使用cURL形成一个请求,使我得到与Firefox相同的响应?

1 个答案:

答案 0 :(得分:0)

  1. 在Firefox中,打开开发人员选项(F12)中的“网络”选项卡,然后打开所需页面的URL。
  2. 记下发送到服务器的请求中的所有请求标头:
  3. 示例:

    OR
    1. 以这种方式将所有标题放在数组中
    2. var obj = { "claimID" : "value", "rederenceDate" : "value" } ....... data: '{ "argPlaceOrderJSON": ' + JSON.stringify(obj) + ' }'

      1. 使用php函数curl_setoption()在请求中设置标题:
      2. Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding gzip, deflate Accept-Language nl,en-US;q=0.7,en;q=0.3 Connection keep-alive Cookie _ga=GA1.2.598213448.1471644637; _gat=1 Host mariannesdelights.be User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0

        这应该产生完全相同的HTTP响应头。​​