Tuesday, 28 May 2013

Direct upload function not working for youtube api

Direct upload function not working for youtube api

I am using youtube api for uploading a video directly on youtube. I downloaded the scripts and integrate on my project. I am not getting any error but it also doesn't show anything. Here is the code :-
public function directUpload($path, $contenttype, $metadata, $filesize = false, $user = 'default')
    {
        if($this->_access !== false)
        {
            $uri = "/{$this->_uris['USER_URI']}/{$user}/uploads";

            $header = "POST {$uri} HTTP/".self::HTTP_1.self::LINE_END;
            //We use a special host for direct uploads.
            $host = 'uploads.gdata.youtube.com';
            $url = http://{$host}{$uri};
            $extra = "GData-Version: 2.0".self::LINE_END;
            //Add the file name to the slug parameter.
            $extra .= "Slug: ".basename($path).self::LINE_END;
            //Create a random boundary string.
            $this->CI->load->helper('string');
            $boundary = random_string();
            $extra .= "Content-Type: multipart/related; boundary=\"{$boundary}\"".self::LINE_END;

            //Build out the data portion of the request
            $data = "--{$boundary}".self::LINE_END;
            $data .= "Content-Type: application/atom+xml; charset=UTF-8".self::LINE_END.self::LINE_END;
            $data .= $metadata.self::LINE_END;
            $data .= "--{$boundary}".self::LINE_END;
            $data .= "Content-Type: ".$contenttype.self::LINE_END;
            $data .= "Content-Transfer-Encoding: binary".self::LINE_END.self::LINE_END;

            $end = self::LINE_END."--{$boundary}--".self::LINE_END;

            //If file size is not set then calculate it
            //NOTE: This may cause memory problems for large videos
            if($filesize === false)$filesize = filesize($path);

            $length = strlen($data) + intval($filesize) + strlen($end);        

            //Calculate the size of the data portion.
            $extra .= "Content-Length: {$length}".self::LINE_END.self::LINE_END;
            $this->_header['Host'] = $host;//Swap the default host
            $start = $this->_build_header($url, $header, $extra, 'POST');
            $this->_header['Host'] = self::HOST;//Revert the default host.
            $start .= $data;

            $handle = null;
            //Connect to the special upload host
            $handle = $this->_connect($host);
            //Write the request header
            $this->_write($handle, $start);
            //Write the file data
            $this->_write_file($handle, $path);
            //Write the ending
            $this->_write($handle, $end);

            $output = $this->_read($handle);

            fclose($handle);
            $handle = null;

            return $output;
        }
        return false;
    }
I tried to print $output but it does not echoed. I have a video on my server that I want to upload on youtube. File size is 45 kb. I am using codeigniter framework and my video is located on a folder resources/video/1.MP4
On controller there is a function direct_upload in that fucntion I have enter all the required keys also with the file path :-
$videoPath = $_SERVER['DOCUMENT_ROOT']."/resources/video/1.MP4";
$videoType = 'video/mp4'; //Th

No comments:

Post a Comment