dpntq48842
2017-09-14 00:56
浏览 838
已采纳

使用PHP CURL下载MP4文件

I am trying to download a video from one of my servers to another of my servers. I was using CURL because copy() did not download the audio from the video. However, CURL downloaded corrupted files (?) and do not. This is how I'm downloading the MP4 file right now:

$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);

Is there anything special for MP4 files to download properly?

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) }

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将视频从我的一台服务器下载到另一台服务器。 我使用的是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文件有没有什么特别的下载? < p> array(26){[“url”] =&gt; string(60)“https://link.com/video.mp4”[“content_type”] =&gt; NULL [“http_code”] =&gt; int(0)[“header_size”] =&gt; int(0)[“request_size”] =&gt; int(0)[“filetime”] =&gt; int(-1)[“ssl_verify_result”] =&gt; int(1)[“redirect_count”] =&gt; int(0)[“total_time”] =&gt; float(0.135745)[“namelookup_time”] =&gt; float(8.4E-5)[“connect_time”] =&gt; float(0.056009)[“pretransfer_time”] =&gt; float(0)[“size_upload”] =&gt; float(0)[“size_download”] =&gt; float(0)[“speed_download”] =&gt; float(0)[“speed_upload”] =&gt; float(0)[“download_content_length”] =&gt; float(-1)[“upload_content_length”] =&gt; float(-1)[“starttransfer_time”] =&gt; float(0)[“redirect_time”] =&gt; float(0)[“redirect_url”] =&gt; string(0)“”[“primary_ip”] =&gt; string(13)“AN-IP:D”[“certinfo”] =&gt; array(0){} [“primary_port”] =&gt; int(443)[“local_ip”] =&gt; string(11)“192.168.0.9”[“local_port”] =&gt; int(57478)}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmu3187 2017-09-14 01:10
    已采纳

    You must open the file in binary mode to ensure file is saved to disk correctly.

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

    Also use fwrite instead of fputs

    fwrite($file, $data);
    

    Check that video.mp4 downloads from the browser correctly. Maybe it is redirecting? If so the add this option.

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    

    If is still does not work then dump out this info and post it.

    var_dump(curl_getinfo($ch));
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题