代码之家  ›  专栏  ›  技术社区  ›  FrankFabregat

使用PHP CURL下载MP4文件

  •  1
  • FrankFabregat  · 技术社区  · 8 年前

    我正在尝试将视频从我的一个服务器下载到另一个服务器。我使用CURL是因为copy()没有从视频中下载音频。但是,CURL下载了损坏的文件(?)不要这样做。以下是我现在下载MP4文件的方式:

    $source = "https://link.com/video.mp4";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $source);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $data = curl_exec ($ch);
    $error = curl_error($ch);
    curl_close ($ch);
    
    $destination = "video/video.mp4";
    $file = fopen($destination, "wb");
    fwrite($file, $data);
    fclose($file);
    

    有什么特别的MP4文件可以正确下载吗?

    array(26) { ["url"]=> string(60) "https://link.com/video.mp4" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(1) ["redirect_count"]=> int(0) ["total_time"]=> float(0.135745) ["namelookup_time"]=> float(8.4E-5) ["connect_time"]=> float(0.056009) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "AN-IP :D" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(11) "192.168.0.9" ["local_port"]=> int(57478) }

    1 回复  |  直到 8 年前
        1
  •  2
  •   GeorgeQ    8 年前

    您必须以二进制模式打开文件,以确保文件正确保存到磁盘。

    $file = fopen($destination, "wb");
    

    也使用 fwrite 而不是FPUT

    fwrite($file, $data);
    

    video.mp4 从浏览器正确下载。也许是重定向?如果是,则添加此选项。

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    

    var_dump(curl_getinfo($ch));
    
    推荐文章