Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP youtube API v3。所有上传的视频都是未知的_Php_Youtube_Youtube Data Api - Fatal编程技术网

PHP youtube API v3。所有上传的视频都是未知的

PHP youtube API v3。所有上传的视频都是未知的,php,youtube,youtube-data-api,Php,Youtube,Youtube Data Api,您好,我正在尝试通过将在cron中运行的php脚本将视频上传到我的youtube频道 首先,我从获取访问令牌开始,通过脚本刷新令牌,并以这种格式获取json响应 { "access_token": "some token", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "some refresh token", "created": 1509147453 } <?ph

您好,我正在尝试通过将在cron中运行的php脚本将视频上传到我的youtube频道

首先,我从获取访问令牌开始,通过脚本刷新令牌,并以这种格式获取json响应

{
    "access_token": "some token",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "some refresh token",
    "created": 1509147453
}


 <?php

$key = file_get_contents('token.txt');

require_once 'vendor/autoload.php';

$OAUTH2_CLIENT_ID = '';
$OAUTH2_CLIENT_SECRET = '';

$videoPath = "video.mp4";
$videoTitle = "Just an Example Title";
$videoDescription = "This is the YouTube video's description";
$videoCategory = "22";
$videoTags = array("first tag","second tag","third tag");

try{
    // Client init
    $client = new Google_Client();
    $client->setClientId($OAUTH2_CLIENT_ID);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');
    $client->setAccessToken($key);
    $client->setClientSecret($OAUTH2_CLIENT_SECRET);

    if ($client->getAccessToken()) {
        /**
         * Check to see if our access token has expired. If so, get a new one and save it to file for future use.
         */
        if($client->isAccessTokenExpired()) {
            $newToken = json_decode($client->getAccessToken());
            $client->refreshToken($newToken->refresh_token);
            file_put_contents($key, $client->getAccessToken());
        }

        $youtube = new Google_Service_YouTube($client);

        // Create a snipet with title, description, tags and category id
        $snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle($videoTitle);
        $snippet->setDescription($videoDescription);
        $snippet->setCategoryId($videoCategory);
        $snippet->setTags($videoTags);
        $snippet->setDefaultLanguage("en");
        $snippet->setDefaultAudioLanguage("en");

        //$recordingDetails = new Google_Service_YouTube_VideoRecordingDetails();
        //$recordingDetails->setLocationDescription("United States of America");
        //$recordingDetails->setRecordingDate("2016-01-20T12:34:00.000Z");
        //$locationdetails = new Google_Service_YouTube_GeoPoint();
        //$locationdetails->setLatitude("38.8833");
        //$locationdetails->setLongitude("77.0167");
        //$recordingDetails->setLocation($locationdetails);

        // Create a video status with privacy status. Options are "public", "private" and "unlisted".
        $status = new Google_Service_YouTube_VideoStatus();
        $status->setPrivacyStatus("public");
        //$status->setPublicStatsViewable(false);
        //$status->setEmbeddable(false); // Google defect still not editable https://code.google.com/p/gdata-issues/issues/detail?id=4861

        // Create a YouTube video with snippet and status
        $video = new Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        //$video->setRecordingDetails($recordingDetails);
        $video->setStatus($status);

        // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
        // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
        $chunkSizeBytes = 1 * 1024 * 1024;

        // Setting the defer flag to true tells the client to return a request which can be called
        // with ->execute(); instead of making the API call immediately.
        $client->setDefer(true);

        // Create a request for the API's videos.insert method to create and upload the video.
        $insertRequest = $youtube->videos->insert("status,snippet,recordingDetails", $video);

        // Create a MediaFileUpload object for resumable uploads.
        $media = new Google_Http_MediaFileUpload(
            $client,
            $insertRequest,
            'video/*',
            null,
            true,
            $chunkSizeBytes
        );
        $media->setFileSize(filesize($videoPath));

        // Read the media file and upload it chunk by chunk.
        $status = false;
        $handle = fopen($videoPath, "rb");
        while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }

        fclose($handle);

        /**
         * Video has successfully been uploaded, now lets perform some cleanup functions for this video
         */
        if ($status->status['uploadStatus'] == 'uploaded') {
            // Actions to perform for a successful upload
        }

        // If you want to make other calls after the file upload, set setDefer back to false
        $client->setDefer(true);

    } else{
        // @TODO Log error
        echo 'Problems creating the client';
    }

} catch(Google_Service_Exception $e) {
    print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
    print "Stack trace is ".$e->getTraceAsString();
}catch (Exception $e) {
    print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
    print "Stack trace is ".$e->getTraceAsString();
}

?>
  <!doctype html>
<html>
<head>
<title>Video Uploaded</title>
</head>
<body>
  <h3>Video Uploaded</h3><ul><li>unknown (xXSqciVm2ks)</li></ul>uploaded value set to 1 too</body>
</html>