I'm using PHP to send videos via Direct Upload to Youtube. It works fine for smaller sized videos but when trying to send a 390 MB video, I get the following error:
PHP Fatal error: Out of memory (allocated 3932160) (tried to allocate 390201902 bytes)
I've tried increasing memory_limit
but that 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've also tried running curl through exec()
but then even weirder things happen:
curl 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 'Authorization: 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: multipart/related; boundary="iUI5C0hzisAHkx9SvaRJ"' -H 'Content-Length: 390193710' -H 'Connection: close' -d /tmp/youtube.xml
/tmp/youtube.xml is where I have saved the data file to upload. Perhaps this usage is wrong?
This will take about 6 minutes so it looks like the file is being sent but then I get an empty reply:
% 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:
I am using OAuth so I cannot use the normal PHP API library for this. I must upload an XML file with the video's binary data included in the XML file as outlined here
I found another person with the same problem who had supplied code to read and send the file in chunks. However, when trying this method, Youtube would return a 411 page saying the "Content-Length" header is required. I am setting the content-length header so this could be a bug. This method uses the fsockopen()
function instead of cURL. [Actually, looking at the code again, I realise I was just separating the headers with "\n" instead of "\r\n." That might be the issue. I will try with the carriage returns as well]
Edit 2:
I think the "\r\n" worked, but now with the code, I am again receiving an empty reply from Youtube.
Any Curl experts out there that can help me get this working? I'm completely stumped by this.