2013-08-02 62 views
0

我在嘗試使用Youtube API更新視頻元數據。我正在使用的API是google-api-php-client。 該API在上傳和刪除過程中工作完美,但是當我想更新視頻信息時,我不知道必須使用哪些參數。
使用YOUTUBE更新視頻google-api-php-client

這是代碼:

try 
     { 

      // Build the Needed Video Information 
      $snippet = new Google_VideoSnippet(); 
      $snippet->setTitle($_POST['videoTitle']); 
      $snippet->setDescription($_POST['videoDescription']); 
      $snippet->setTags(array($_POST['videoTags'])); 
      $snippet->setCategoryId(22); 

      // Set the Video Info and Status in the Main Tag 
      $video = new Google_Video(); 
      $video->setSnippet($snippet); 

      // Send the video to the Google Youtube API 
      $created_file = $youtube->videos->update('snippet', $video, array(
       WHAT PARAMETERS!!!???!! 
      )); 

      // Get the information of the uploaded video 
      print_r($createdFile); 
     } 
     catch (Exception $ex) 
     { 
      echo $ex; 
     } 

有人知道嗎? ?我想繼續使用這個API,因爲我所有的應用都使用它。

謝謝你在先進。

+0

我想,我必須以某種方式通過視頻ID,但我不知道如何將它傳遞。 – cmaciasg

回答

1

更新視頻,首先你需要使用videos-> id爲VIDEO_ID列表,然後更新意的部分,全部包住,直到子部分的視頻本身到谷歌的對象,併發送更新請求。 (我們將努力使這個更簡單)

這裏有一個更新標籤樣本,最終更新視頻,你可以使用它。

/** 
* This sample adds new tags to a YouTube video by: 
* 
* 1. Retrieving the video with "youtube.videos.list" method setting the "id" parameter 
* 2. Appending new tags to Video Resource's snippet.tags[] list 
* 3. Updating the video itself via youtube.videos.update API call, supplying a new video via a 
* binary upload to replace the old one. 
* 
* @author Ibrahim Ulukaya 
*/ 

// Call set_include_path() as needed to point to your client library. 
require_once 'Google_Client.php'; 
require_once 'contrib/Google_YouTubeService.php'; 
session_start(); 

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console 
<http://code.google.com/apis/console#access> 
For more information about using OAuth2 to access Google APIs, please visit: 
<https://developers.google.com/accounts/docs/OAuth2> 
Please ensure that you have enabled the YouTube Data API for your project. */ 
$OAUTH2_CLIENT_ID = 'REPLACE ME'; 
$OAUTH2_CLIENT_SECRET = 'REPLACE ME'; 

$client = new Google_Client(); 
$client->setClientId($OAUTH2_CLIENT_ID); 
$client->setClientSecret($OAUTH2_CLIENT_SECRET); 
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 
    FILTER_SANITIZE_URL); 
$client->setRedirectUri($redirect); 

// YouTube object used to make all Data API requests. 
$youtube = new Google_YoutubeService($client); 

if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 

    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: ' . $redirect); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

// Check if access token successfully acquired 
if ($client->getAccessToken()) { 
    try{ 

    // REPLACE with the video ID that you want to update 
    $videoId = "VIDEO_ID"; 

    // Create a video list request 
    $listResponse = $youtube->videos->listVideos("snippet", 
     array('id' => $videoId)); 

    $videoList = $listResponse['items']; 
    if (empty($videoList)) { 
     $htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId); 
    } else { 
     // Since a unique video id is given, it will only return 1 video. 
     $video = $videoList[0]; 
     $videoSnippet = $video['snippet']; 

     $tags = $videoSnippet['tags']; 

     // $tags is null if the video didn't have any tags, so we will check for this and 
     // create a new list if needed 
     if (is_null($tags)) { 
     $tags = array("tag1", "tag2"); 
     } else { 
     array_push($tags, "tag1", "tag2"); 
     } 

     // Construct the Google_Video with the updated tags, hence the snippet 
     $updateVideo = new Google_Video($video); 
     $updateSnippet = new Google_VideoSnippet($videoSnippet); 
     $updateSnippet->setTags($tags); 
     $updateVideo -> setSnippet($updateSnippet); 

     // Create a video update request 
     $updateResponse = $youtube->videos->update("snippet", $updateVideo); 

     $responseTags = $updateResponse['snippet']['tags']; 


    $htmlBody .= "<h3>Video Updated</h3><ul>"; 
    $htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>', 
     array_pop($responseTags), array_pop($responseTags), 
     $videoId, $updateResponse['snippet']['title']); 

    $htmlBody .= '</ul>'; 
    } 
    } 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())); 
    } 

    $_SESSION['token'] = $client->getAccessToken(); 
    } else { 
     // If the user hasn't authorized the app, initiate the OAuth flow 
     $state = mt_rand(); 
     $client->setState($state); 
     $_SESSION['state'] = $state; 

     $authUrl = $client->createAuthUrl(); 
     $htmlBody = <<<END 
    <h3>Authorization Required</h3> 
    <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> 
END; 
    } 
    ?> 

    <!doctype html> 
    <html> 
    <head> 
    <title>Video Updated</title> 
    </head> 
    <body> 
     <?=$htmlBody?> 
    </body> 
    </html> 
+0

謝謝你的迴應,易卜拉欣。我不知道他必須做「列表視頻」步驟。
奇怪的是,該方法「更新」沒有一個id參數,是不是? 我要試一下。 – cmaciasg

+0

另一個問題,如果你請易卜拉欣,類別視頻是可更新的? – cmaciasg

+0

是的。由於您提供了視頻對象,因此它會從中獲取ID。 –