upload video to youtube using php -


i beginner in php. want upload video on youtube using php. http error 404 occurs while running demo . here demo link used .

http://www.codexworld.com/upload-video-to-youtube-using-php/

and created oauthclientid , oauthclientsecret google accounts. , put under config file.

<?php      // oauth configuration     $oauthclientid = 'xxxxx';     $oauthclientsecret = 'xxxxxxx';     $baseuri = 'http://example.com/upload-video-to-youtube-using-php/';     $redirecturi = 'http://example.com/upload-video-to-youtube-using-php/youtube_upload.php';      define('oauth_client_id',$oauthclientid);     define('oauth_client_secret',$oauthclientsecret);     define('redirect_uri',$redirecturi);     define('base_uri',$baseuri);      // include google client libraries     require_once 'src/google/autoload.php';      require_once 'src/google/client.php';     require_once 'src/google/service/youtube.php';     session_start();      $client = new google_client();     $client->setclientid(oauth_client_id);     $client->setclientsecret(oauth_client_secret);     $client->setscopes('https://www.googleapis.com/auth/youtube');     $client->setredirecturi(redirect_uri);      // define object used make api requests.     $youtube = new google_service_youtube($client);  ?> 

and here redirect url code

<?php //echo "www";exit; require_once 'config.php'; require_once 'includes/db.php'; // create object of class db. $db = new db;  if(isset($_request['videosubmit'])){     $videotitle = $_request['title'];     $videodesc = $_request['description'];     $videotags = $_request['tags'];      if($_files["videofile"]["name"] != ''){         $filesize = $_files['videofile']['size'];         $filetype = $_files['videofile']['type'];         $filename = str_shuffle('nityanandamaity').'-'.basename($_files["videofile"]["name"]);         $targetdir = "videos/";         $targetfile = $targetdir . $filename;         $allowedtypearr = array("video/mp4", "video/avi", "video/mpeg", "video/mpg", "video/mov", "video/wmv", "video/rm");         if(in_array($filetype, $allowedtypearr)) {             if(move_uploaded_file($_files['videofile']['tmp_name'], $targetfile)) {                 $videofilepath = $targetfile;             }else{                 header('location:'.base_uri.'index.php?err=ue');                 exit;             }         }else{             header('location:'.base_uri.'index.php?err=fe');             exit;         }          // insert video data         $db->insert($videotitle,$videodesc,$videotags,$videofilepath);      }else{         header('location:'.base_uri.'index.php?err=bf');         exit;     } }  // last video data $result = $db->getlastrow();    /*  * can acquire oauth 2.0 client id , client secret  * google developers console <https://console.developers.google.com/>  * more information using oauth 2.0 access google apis, please see:  * <https://developers.google.com/youtube/v3/guides/authentication>  * please ensure have enabled youtube data api project.  */    if (isset($_get['code'])) {     if (strval($_session['state']) !== strval($_get['state'])) {       die('the session state did not match.');     }      $client->authenticate($_get['code']);     $_session['token'] = $client->getaccesstoken();      header('location: ' . redirect_uri); }  if (isset($_session['token'])) {     $client->setaccesstoken($_session['token']); }  $htmlbody = ''; // check ensure access token acquired. if ($client->getaccesstoken()) {   try{     // replace value path file uploading.     $videopath = $result['video_path'];      // create snippet title, description, tags , category id     // create asset resource , set snippet metadata , type.     // example sets video's title, description, keyword tags, ,     // video category.     $snippet = new google_service_youtube_videosnippet();     $snippet->settitle($result['video_title']);     $snippet->setdescription($result['video_description']);     $snippet->settags(explode(",",$result['video_tags']));      // numeric video category. see     // https://developers.google.com/youtube/v3/docs/videocategories/list      $snippet->setcategoryid("22");      // set video's status "public". valid statuses "public",     // "private" , "unlisted".     $status = new google_service_youtube_videostatus();     $status->privacystatus = "public";      // associate snippet , status objects new video resource.     $video = new google_service_youtube_video();     $video->setsnippet($snippet);     $video->setstatus($status);      // specify size of each chunk of data, in bytes. set higher value     // reliable connection fewer chunks lead faster uploads. set lower     // value better recovery on less reliable connections.     $chunksizebytes = 1 * 1024 * 1024;      // setting defer flag true tells client return request can called     // ->execute(); instead of making api call immediately.     $client->setdefer(true);      // create request api's videos.insert method create , upload video.     $insertrequest = $youtube->videos->insert("status,snippet", $video);      // create mediafileupload object resumable uploads.     $media = new google_http_mediafileupload(         $client,         $insertrequest,         'video/*',         null,         true,         $chunksizebytes     );     $media->setfilesize(filesize($videopath));      // read media file , upload it.     $status = false;     $handle = fopen($videopath, "rb");     while (!$status && !feof($handle)) {       $chunk = fread($handle, $chunksizebytes);       $status = $media->nextchunk($chunk);     }     fclose($handle);      // if want make other calls after file upload, set setdefer false     $client->setdefer(false);      // update youtube video id database     $db->update($result['video_id'],$status['id']);     // delete video file local folder     @unlink($result['video_path']);      $htmlbody .= "<p class='succ-msg'>video have been uploaded successfully.</p><ul>";     $htmlbody .= '<embed width="400" height="315" src="https://www.youtube.com/embed/'.$status['id'].'"></embed>';     $htmlbody .= '<li><b>title: </b>'.$status['snippet']['title'].'</li>';     $htmlbody .= '<li><b>description: </b>'.$status['snippet']['description'].'</li>';     $htmlbody .= '<li><b>tags: </b>'.implode(",",$status['snippet']['tags']).'</li>';     $htmlbody .= '</ul>';     $htmlbody .= '<a href="logout.php">logout</a>';    } catch (google_serviceexception $e) {     $htmlbody .= sprintf('<p>a service error occurred: <code>%s</code></p>',         htmlspecialchars($e->getmessage()));   } catch (google_exception $e) {     $htmlbody .= sprintf('<p>an client error occurred: <code>%s</code></p>', htmlspecialchars($e->getmessage()));     $htmlbody .= 'please reset session <a href="logout.php">logout</a>';   }    $_session['token'] = $client->getaccesstoken(); } else {     // if user hasn't authorized app, initiate oauth flow     $state = mt_rand();     $client->setstate($state);     $_session['state'] = $state;      $authurl = $client->createauthurl();     $htmlbody = <<<end     <h3>authorization required</h3>     <p>you need <a href="$authurl">authorize access</a> before proceeding.<p> end;  }  ?>  <!doctype html> <html> <head>     <title>upload video youtube using php</title>     <link rel="stylesheet" type="text/css" href="css/style.css"/> </head> <body>     <div class="youtube-box">         <h1>upload video youtube using php</h1>         <div class="video-up">             <a href="<?php echo base_uri; ?>">new upload</a>         </div>         <div class="content">             <?php echo $htmlbody; ?>         </div>     </div>  </body> </html> 


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -