PHP + 유튜브(youtube) 동영상 업로드 연동 소스

by 조쉬 posted Jan 21, 2021
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
<form method="post" name="form1">
<input type="hidden" name="ucc_url">
제목 : <input style="WIDTH: 500px" maxlength="100" name="title">
영상첨부 : <div onclick="youtube_open();" class="btn_100">동영상 첨부</div>
</form>
<div><input onclick="check_in();" type="button" value="글저장"></div>
<form method="post" name="form2">
<input type="hidden" name="title">
</form>
<script language="javascript">
    function youtube_open(){
        if(document.form1.title.value==''){
            alert('제목을 먼저 입력해주세요.\n유튜브 영상에 표시될 제목입니다.');
            document.form1.title.focus();
            return;
        }
        f = document.form2;
         window.open('','youtPop','width=400,height=300');
         f.target = 'youtPop';
         f.action = 'youtube_upload.php';
         f.title.value=document.form1.title.value;
         f.submit();
    }
                
               function check_in(){
                  if(document.form1.ucc_url.value==''){
                     alert('영상을 첨부해주세요.');
                     return;
                  }
                  document.form1.action='ok.php';
                  document.form1.submit();
              }
</script>
 

 

youtube_upload.php

<!--?php
$youtube_email = "유튜브 로그인 아이디"; // Change this to your youtube sign in email.
$youtube_password = "유튜브 로그인 패스워드"; // Change this to your youtube sign in password.
  
$postdata = "Email=".$youtube_email."&Passwd=".$youtube_password."&service=youtube&source=Example";
$curl = curl_init("https://www.google.com/youtube/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($curl);
curl_close($curl);
  
list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));
  
 $_REQUEST['title'] = iconv("euc-kr", "utf-8", $_REQUEST['title']);  // 외국 자료 원본소스에서 한글처리 때문에 추가
  
$youtube_video_title = $_REQUEST['title']; // This is the uploading video title.
$youtube_video_description = $_REQUEST['title']; // This is the uploading video description.
$youtube_video_category = "Entertainment"; // This is the uploading video category.   //카테고리는 정책적으로 지정된것중에서만 써야되는듯...
$youtube_video_keywords = $_REQUEST['title']; // This is the uploading video keywords.
  
$data = '<?xml version="1.0"?-->
            <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
              <media:group>
                <media:title type="plain">'.$youtube_video_title.'</media:title>
                <!--?xml:namespace prefix = "media" /--><media:description type="plain">'.$youtube_video_description.'</media:description>
                <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtube_video_category.'</media:category>
                <media:keywords>'.$youtube_video_keywords.'</media:keywords>
               
            </media:group></entry>';
  
$key = "발급받은 유튜브 개발자 키"; // Get your key here: http://code.google.com/apis/youtube/dashboard/.
  
$headers = array("Authorization: GoogleLogin auth=".$authvalue,
                 "GData-Version: 2",
                 "X-GData-Key: key=".$key,
                 "Content-length: ".strlen($data),
                 "Content-Type: application/atom+xml; charset=UTF-8");
  
$curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
  
$response = simplexml_load_string(curl_exec($curl));
curl_close($curl);
?>
<!--?php
$nexturl = "리턴값을 받을 페이지 풀url..여기서는 일단 http://도메인/upload_ok.php"; // This parameter specifies the URL to which YouTube will redirect the user's browser when the user uploads his video file.
?-->
 
 
 
 
<SCRIPT type=text/javascript>
  function checkForFile() {
    if (document.form_y.file.value!=='') {
        document.form_y.submit();
    }else{
        alert('영상을 첨부해주세요.');
    }
  }
</SCRIPT>
 
 
<form enctype="multipart/form-data" method="post" name="form_y" action="<?php echo($response->url); ?>?nexturl=<?php echo(urlencode($nexturl)); ?>">
  파일 : <input id="file" type="file" name="file">
  제목 : <!--? echo($youtube_video_title); ?-->
  <input type="hidden" value="<?php echo($response->token); ?>" name="token">
  <input onclick="checkForFile();" type="button" value="유튜브로 전송">
</form>
※ 업로드창이 자동으로 닫힐 때까지 창을 닫지 마세요.
※ 업로드가 완료되면 이 창은 자동으로 닫힙니다.
 

 

 

<!--?
    $ucc_url = $_REQUEST["id"];
    $ucc_status = $_REQUEST["status"];
     
    if($ucc_status == "200"){
?-->
        <SCRIPT language=javascript>
        <!--
            opener.document.form1.ucc_url.value='<?=$ucc_url?>';
            window.close();
        //-->
        </SCRIPT>
<!--?
    }else{
?-->
        <SCRIPT language=javascript>
        <!--
            alert("실패");
            window.close();
        //-->
        </SCRIPT>
<!--?
    }  
?-->

 

썸네일소스

<iframe width="640" height="360" src="https://www.youtube.com/embed/<!--?=$ucc_url?-->" frameborder="0" allowfullscreen></iframe>
<img src="http://i2.ytimg.com/vi/%3C?=$ucc_url?>/default.jpg">