Downloading large video via Youtube API causing low memory

I use PHP to send videos via direct upload to Youtube. It works great for smaller videos, but when I try to send 390 MB of video, I get the following error:

PHP Fatal error: Out of memory (3932160 allocated) (tried to allocate 390201902 bytes)

I tried to increase memory_limit, but it does not help.

    if ($isFile) {
        ini_set('memory_limit', '2G')
        $data = file_get_contents($data);
    }

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $out = curl_exec($ch);
    curl_close($ch);

    return $out;

I also tried running curl through exec(), but then even weirder things happen:

http://uploads.gdata.youtube.com/feeds/api/users/default/uploads-H 'POST/feeds/api/users/default/uploads HTTP/1.1 '-H' Host: uploads.gdata.youtube.com '-H ': OAuth [snip oauth info] "'-H' GData-Version: 2 '-H" X-GData-Client: www.mywebsite.com "-H 'X-GData-Key: key = [snip]' -H 'Slug: video.AVI' -H 'Content-Type: /; border =" iUI5C0hzisAHkx9SvaRJ" '-H 'Content-Length: 390193710' -H ': close' -d /tmp/youtube.xml

/tmp/youtube.xml - , . , ?

6 , , , :

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed

 0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
 0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
 ...
 0     0    0     0    0     0      0      0 --:--:--  0:06:00 --:--:--     0
curl: (52) Empty reply from server

EDIT:

OAuth, API PHP . XML , XML ,

I , . Youtube 411, "Content-Length". , . fsockopen() cURL. [ , , , "\n" "\ r\n". . ]

2:

, "\ r\n" , , Youtube.

Curl, ? .

+3
4

. curl IMHO . . : http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

API youtube, , html, . , cURL, POST .

(~ 4 ), HTTP- API PHP: POST ( fwrite() stream_copy_to_stream() ). , http- youtube ( stream_copy_to_stream()).

, 4 , .

EDIT:

-php--mashup ;)

$xmlAPIRequest = /* fill with the XML-API-Request */
$boundaryString = /* fill me with some random data */

// create temporary file handle
$pdh = tmpfile();
fwrite($pdh, "--$boundaryString\r\n");
fwrite($pdh, "Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n");
fwrite($pdh, $xmlAPIRequest."\r\n");
fwrite($pdh, "--$boundaryString\r\n");
fwrite($pdh, "Content-Type: <video_content_type>\r\nContent-Transfer-Encoding: binary\r\n\r\n");

$videoFile = fopen("/path/to/video", "r");
stream_copy_to_stream($videoFile, $pdh);
fclose($videoFile);

fwrite($pdh, "--$boundaryString--\r\n");
/* not quite sure, whether there needs to be another linebreak before the boundary string */

$info = fstat($pdh);
rewind($pdh);

$contentLength = $info['size'];

$conn = fsockopen("hostname", 80);
/* write http request to $conn and use $contentLength for Content-Length header */
/* after last header you put another line break to tell them that now the body follows */

// write post data from stream to stream
stream_copy_to_stream($pdh, $conn);

// ... process response... etc...

, , , , .;)

+3

:

ini_set('memory_limit', -1);

?

0

$filename = "--REMOTE FILE--";
$localfile = "/storage/local.flv";

$handle = fopen($filename, "r");

while ($contents = fread($handle, 10485760)) { // thats 10 MB

$localhandle = fopen($localfile, "a");
fwrite ($localhandle, $contents);
fclose($localhandle);

}

fclose($handle);
0

, :

public function uploadVideo(Model_Row_Video $video)
{
        $request = $this->getOAuthRequest(self::URL_UPLOAD, 'POST');

        $boundary = 'RANDOM BOUNDARY STRING';

        $videoFile = $video->getAbsolutePath();
        $contentType = $this->getContentType($videoFile);

        $xml = $this->getAtom($video);
        $data = <<<EOD
--{$boundary}
Content-Type: application/atom+xml; charset=UTF-8

{$xml}
--{$boundary}
Content-Type: {$contentType}
Content-Transfer-Encoding: binary\r\n\r\n
EOD;
        $footer = "\r\n--{$boundary}--\r\n";

        $pdh = tmpfile();
        fwrite($pdh, $data);
        $f_video = fopen($videoFile, "r");
        stream_copy_to_stream($f_video, $pdh);
        fclose($f_video);
        fwrite($pdh, $footer);

        $info = fstat($pdh);

        $headers = array(
            "POST /feeds/api/users/default/uploads HTTP/1.1",
            'Host: uploads.gdata.youtube.com',
            $request->to_header(),
            'GData-Version: 2',
            'X-GData-Client: ' . self::CONSUMER_KEY,
            'X-GData-Key: key=' . self::DEVELOPER_KEY,
            'Slug: ' . $video['local'],
            'Content-Type: multipart/related; boundary="' . $boundary . '"',
            'Content-Length: ' . $info['size'],
            'Connection: close'
        );

        $headers_str = implode("\r\n", $headers) . "\r\n\r\n";
        rewind($pdh);

        $conn = fsockopen('uploads.gdata.youtube.com', 80, $errno, $errstr, 30);
        fputs($conn, $headers_str);
        stream_copy_to_stream($pdh, $conn);

        $return = '';
        while (!feof($conn)) {
            $return .= fgets($conn, 128);
        }

        fclose($conn);

        echo "errno: $errno\n";
        echo "errstr: $errstr\n";
        $out = strstr($return, '<?xml');

        $xml = simplexml_load_string($out);

        if (!$xml) {
            echo $out;
        }

        $xml->registerXPathNamespace('yt','http://gdata.youtube.com/schemas/2007');
        $id = $xml->xpath('//yt:videoid');

        return $id[0];
}
0

Source: https://habr.com/ru/post/1729756/


All Articles