dougou8458 2012-07-03 12:50
浏览 113
已采纳

忽略内容类型:CURL中的应用程序/强制下载

I need to read the size of a file but the server is forcing me to download it first. I note one of the response headers is Content-Type: application/force-download and that seems to bypass my curl inputs...

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
curl_exec($ch);
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);

Any ideas?

  • 写回答

2条回答 默认 最新

  • dongyu4908 2012-07-03 13:16
    关注

    Curl conforms with the force-download header, but file_get_contents can be used to limit the file download download to only 1 byte. This solved the problem!

    $postdata = http_build_query(
        array(
            'username' => "",
            'password' => ""
        )
    );
    $params = array(
        'http' => array
        (
          'method' => 'POST',
          'header'=>"Content-type: application/x-www-form-urlencoded
    ",
          'content' => $postdata
        )
    );
    $ctx = stream_context_create($params);
    file_get_contents($url,false,$ctx,0,1);
    $size = str_replace("Content-Length: ","",$http_response_header[4]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件