title: Upload video to Youtube using Youtube API V3 and PHP
body: <p>I am trying to upload a video to Youtube using PHP. I am using Youtube API v3 and I am using the latest checked out source code of Google API PHP Client library.<br>
  I am using the sample code given on<br>
  <a href="https://code.google.com/p/google-api-php-client/" rel="noreferrer">https://code.google.com/p/google-api-php-client/</a> to perform the authentication. The authentication goes through fine but when I try to upload a video I get <code>Google_ServiceException</code> with error code 500 and message as null. </p>
  
  <p>I had a look at the following question asked earlier:
  <a href="https://stackoverflow.com/questions/13177266/upload-video-to-youtube-using-php-client-library-v3">Upload video to youtube using php client library v3</a> But the accepted answer doesn't describe how to specify file data to be uploaded.<br>
  I found another similar question <a href="https://stackoverflow.com/questions/13954601/uploading-file-with-youtube-api-v3-and-php">Uploading file with Youtube API v3 and PHP</a>, where in the comment it is mentioned that categoryId is mandatory, hence I tried setting the categoryId in the snippet but still it gives the same exception.</p>
  
  <p>I also referred to the Python code on the the documentation site ( <a href="https://developers.google.com/youtube/v3/docs/videos/insert" rel="noreferrer">https://developers.google.com/youtube/v3/docs/videos/insert</a> ), but I couldn't find the function next_chunk in the client library. But I tried to put a loop (mentioned in the code snippet) to retry on getting error code 500, but in all 10 iterations I get the same error.</p>
  
  <p>Following is the code snippet I am trying:</p>
  
  <pre class="lang-php prettyprint-override"><code>$youTubeService = new Google_YoutubeService($client);
  if ($client-&gt;getAccessToken()) {
      print "Successfully authenticated";
      $snippet = new Google_VideoSnippet();
      $snippet-&gt;setTitle = "My Demo title";
      $snippet-&gt;setDescription = "My Demo descrition";
      $snippet-&gt;setTags = array("tag1","tag2");
      $snippet-&gt;setCategoryId(23); // this was added later after refering to another question on stackoverflow
  
      $status = new Google_VideoStatus();
      $status-&gt;privacyStatus = "private";
  
      $video = new Google_Video();
      $video-&gt;setSnippet($snippet);
      $video-&gt;setStatus($status);
  
      $data = file_get_contents("video.mp4"); // This file is present in the same directory as the code
      $mediaUpload = new Google_MediaFileUpload("video/mp4",$data);
      $error = true;
      $i = 0;
  
      // I added this loop because on the sample python code on the documentation page
      // mentions we should retry if we get error codes 500,502,503,504
      $retryErrorCodes = array(500, 502, 503, 504);
      while($i &lt; 10 &amp;&amp; $error) {
          try{
              $ret = $youTubeService-&gt;videos-&gt;insert("status,snippet", 
                                                     $video, 
                                                     array("data" =&gt; $data));
  
              // tried the following as well, but even this returns error code 500,
              // $ret = $youTubeService-&gt;videos-&gt;insert("status,snippet", 
              //                                        $video, 
              //                                        array("mediaUpload" =&gt; $mediaUpload); 
              $error = false;
          } catch(Google_ServiceException $e) {
              print "Caught Google service Exception ".$e-&gt;getCode()
                    . " message is ".$e-&gt;getMessage();
              if(!in_array($e-&gt;getCode(), $retryErrorCodes)){
                  break;
              }
              $i++;
          }
      }
      print "Return value is ".print_r($ret,true);
  
      // We're not done yet. Remember to update the cached access token.
      // Remember to replace $_SESSION with a real database or memcached.
      $_SESSION['token'] = $client-&gt;getAccessToken();
  } else {
      $authUrl = $client-&gt;createAuthUrl();
      print "&lt;a href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
  }
  </code></pre>
  
  <p>Is it something that I am doing wrong? </p>