如何发出与Firefox产生相同响应头的cURL请求 [英] How to make a cURL request that produces the same response headers as Firefox

查看:90
本文介绍了如何发出与Firefox产生相同响应头的cURL请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I browse to a page with Firefox and click a download link, the following headers are shown when I inspect the request in network inspector:


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访问相同的地址时,会显示以下信息:

However, when I use cURL to visit the same address, I get this:


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相同的响应的请求?

How can I form a request with cURL that gives me the same response as Firefox?

推荐答案


  1. 在Firefox中,打开开发人员选项(F12)中的网络选项卡,然后打开所需页面的URL 。

  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




  1. 以这种方式将所有标头放入数组中

$ headers = array('HeaderName:HeaderValue','HeaderName2:HeaderValue2');


  1. 使用php函数curl_setoption()设置请求中的标头:

curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

应该产生完全相同的HTTP-Response标头。

That should produce the exact same HTTP-Response headers.

这篇关于如何发出与Firefox产生相同响应头的cURL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆