douzhangli9563 2016-08-19 20:53
浏览 369
已采纳

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

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

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

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

  • 写回答

1条回答 默认 最新

  • dougui4325 2016-08-19 22:17
    关注
    1. In Firefox, open up the Net tab in the developer options(F12) and open the URL of the page you need.
    2. Take note of all the Request Headers in the request sent to the server:

    Example:

    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. Put all the headers in an array in this way

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

    1. Use the php function curl_setoption() to set the headers in the request:

    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);

    That should produce the exact same HTTP-Response headers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化