2015-05-23 3 views
2

Я пытаюсь создать php-скрипт, который загружает youtube & видео facebook на мой сервер.Php curl для загрузки видео в facebook

Это мой код

$url = "video_url"; 

ini_set('max_execution_time', 0); 
ini_set('memory_limit', '1024M'); 

$fp = fopen ('../cache_temp/file.mp4', 'w+'); 
$ch = curl_init(str_replace(" ","%20",$url)); 
curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

Этот код прекрасно с youtube видео работает .. но с facebook видео он не работает, он только пишет 0 byte файл на моем диске.

Это прямая ссылка на facebook видео

https://video-cai1-1.xx.fbcdn.net/hvideo-xpa1/v/t42.1790-2/1192561_641914572487241_56890_n.mp4?oh=c43e04a7ec841e98a840b251964b4a6c&oe=5560DA5C

Может кто-нибудь сказать мне, как я могу скачать это видео с помощью PHP?

ответ

2

Ok я использовал эту функцию, и она отлично работает

function copyfile_chunked($infile, $outfile) 
{ 
    $chunksize = 100 * (1024 * 1024); // 10 Megs 

    /** 
    * parse_url breaks a part a URL into it's parts, i.e. host, path, 
    * query string, etc. 
    */ 
    $parts = parse_url($infile); 
    $i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5); 
    $o_handle = fopen($outfile, 'wb'); 

    if ($i_handle == false || $o_handle == false) { 
     return false; 
    } 

    if (!empty($parts['query'])) { 
     $parts['path'] .= '?' . $parts['query']; 
    } 

    /** 
    * Send the request to the server for the file 
    */ 
    $request = "GET {$parts['path']} HTTP/1.1\r\n"; 
    $request .= "Host: {$parts['host']}\r\n"; 
    $request .= "User-Agent: Mozilla/5.0\r\n"; 
    $request .= "Keep-Alive: 115\r\n"; 
    $request .= "Connection: keep-alive\r\n\r\n"; 
    fwrite($i_handle, $request); 

    /** 
    * Now read the headers from the remote server. We'll need 
    * to get the content length. 
    */ 
    $headers = array(); 
    while (!feof($i_handle)) { 
     $line = fgets($i_handle); 
     if ($line == "\r\n") 
      break; 
     $headers[] = $line; 
    } 

    /** 
    * Look for the Content-Length header, and get the size 
    * of the remote file. 
    */ 
    $length = 0; 
    foreach ($headers as $header) { 
     if (stripos($header, 'Content-Length:') === 0) { 
      $length = (int) str_replace('Content-Length: ', '', $header); 
      break; 
     } 
    } 

    /** 
    * Start reading in the remote file, and writing it to the 
    * local file one chunk at a time. 
    */ 
    $cnt = 0; 
    while (!feof($i_handle)) { 
     $buf = ''; 
     $buf = fread($i_handle, $chunksize); 
     $bytes = fwrite($o_handle, $buf); 
     if ($bytes == false) { 
      return false; 
     } 
     $cnt += $bytes; 

     /** 
     * We're done reading when we've reached the conent length 
     */ 
     if ($cnt >= $length) 
      break; 
    } 

    fclose($i_handle); 
    fclose($o_handle); 
    return $cnt; 
} 
Смежные вопросы