Php 如何生成与Firefox相同的响应头的cURL请求
当我使用Firefox浏览页面并单击下载链接时,在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 连接:保持活力 内容处置:附件;filename=“example\u file.mp3” 内容长度:35181829 内容传输编码:二进制 内容类型:音频/mpeg 日期:2016年8月19日星期五18:19:02 GMT 保持活动状态:超时=60 服务器: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 连接:保持活力 内容长度:1918 内容类型:text/html;字符集=UTF-8 日期:2016年8月19日星期五20:46:23 GMT 保持活动状态:超时=60 服务器:nginx X-Powered-By:PHP/5.4.45 如何使用cURL生成一个与Firefox相同的响应的请求Php 如何生成与Firefox相同的响应头的cURL请求,php,http,firefox,curl,http-headers,Php,Http,Firefox,Curl,Http Headers,当我使用Firefox浏览页面并单击下载链接时,在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
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
$headers=array('HeaderName:HeaderValue','HeaderName:HeaderValue 2')代码>
使用php函数curl_setoption()设置请求中的标题:
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers)代码>
这将产生完全相同的HTTP响应头
在Firefox中,打开开发者选项(F12)中的Net选项卡,并打开所需页面的URL
注意发送到服务器的请求中的所有请求头:
例如:
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
以这种方式将所有标头放入一个数组中
$headers=array('HeaderName:HeaderValue','HeaderName:HeaderValue 2')代码>
使用php函数curl_setoption()设置请求中的标题:
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers)代码>
这将产生完全相同的HTTP响应头。curl不能在响应中强制使用头。这些都是由产生响应的任何东西生成的。如果希望curl的行为与FF相同,那么curl必须发送与FF完全相同的内容。curl不能在响应中强制使用头。这些都是由产生响应的任何东西生成的。如果希望curl的行为与FF相同,那么curl必须发送与FF完全相同的内容。