来自私人视频的Vimeo缩略图

时间:2017-08-01 02:09:44

标签: php thumbnails vimeo vimeo-api

我已经阅读了API文档,但我迷路了。

我想做的就是从私人视频中获取缩略图。

我没有构建应用程序,但要获得我需要的密钥,我必须包含应用程序的网站,徽标和应用程序的返回URL。

我再次需要一个缩略图。

我不是API的新手,但我真的需要一些示例PHP代码来帮助我开始,以及如何设置应用程序来获取我需要的密钥。

据我所知,我应该使用oembed,这看起来很简单,但我需要帮助进行身份验证。

2 个答案:

答案 0 :(得分:9)

创建App

首先,您需要在您帐户下的Vimeo开发者网站上创建一个应用程序。不要过多担心应用程序的网站,徽标和应用程序的返回URL。

您只需输入localhost作为网站网址和返回网址,您就可以为徽标上传自己的图片(请注意图片不是必需的)。

但我建议填写"说明"最适合你想要做的事情。

这三项要求仅适用于特定身份验证,而您不想要这样做。

请确保回答以下问题:

  

您是否会向用户收费以使用您的应用?   您的集成是否会得到广告支持?

回答" Web Server"问题:

  

这个应用程序将运行哪些平台?

如果您的应用需要 UPLOAD 访问权限,他们可能会问一个问题。确保您回答,因为请求UPLOAD访问权限的应用程序会通过审核。

创建令牌

转到您应用的" 身份验证"标签

基本上,我们只是跳过使用回调网址的身份验证,只是直接创建一个包含以下范围的令牌:

PUBLIC,PRIVATE public private scopes vimeo token 不要复制我的令牌,我已将其删除。

创建令牌后,请确保将其复制并将其存储在某处。刷新页面后,您将无法再次获取令牌。请注意,您只使用PUBLIC和PRIVATE范围。由于您只是要求提供信息,这就足够了。

此外,您需要在App的身份验证标签下提供的客户端标识符客户端机密

使用Official Vimeo PHP LIbrary

使用客户端ID,客户端机密和令牌进行基本身份验证。

//Where $client_id and $client_secrets are variables with your vimeo client id 
//and secrets that you copied from the app you created on Vimeo.
$vimeoLib = new \Vimeo\Vimeo($client_id, $client_secrets);

//Where $token is the variable containing the token you generated earlier
$vimeoLib->setToken($token);

获取所有缩略图

Vimeo将生成多个缩略图(不同大小)。如果您不知道视频的缩略图ID是什么,那么我们必须先审核所有视频数据。

我们首先会看到如何从单个视频返回的所有数据中获取缩略图。

为此,我们将使用此API:

GET https://api.vimeo.com/videos/{video_id}

返回以下内容:

+------------------+--------------------------------------------------+
| Http Status Code | Explanation                                      |
+------------------+--------------------------------------------------+
| 200 Ok           |                                                  |
+------------------+--------------------------------------------------+
| 404 not found    | If the video cannot be found.                    |
+------------------+--------------------------------------------------+

请确保您知道您的vimeo ID是什么。

//Where $vimeo_id is a variable containing your video's vimeo Id
$response = $vimeoLib->request("/me/videos/".$vimeo_id);

缩略图的响应如下所示:

{
    //I've replaced information regarding my videos with {SOME_DESCRIPTIVE_TEXT}
    //More info here
    "pictures": {
            "uri": "/videos/{VIMEO_ID_HERE}/pictures/{THUMBNAIL_ID_HERE}",
            "active": true,
            "type": "custom",
            "sizes": [
                {
                    "width": 100,
                    "height": 75,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_100x75.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_100x75.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                },
                {
                    "width": 200,
                    "height": 150,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_200x150.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_200x150.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                },
                {
                    "width": 295,
                    "height": 166,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_295x166.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_295x166.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                },
                {
                    "width": 640,
                    "height": 360,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_640x360.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_640x360.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                },
                {
                    "width": 960,
                    "height": 540,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_960x540.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_960x540.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                },
                {
                    "width": 1920,
                    "height": 1080,
                    "link": "https://i.vimeocdn.com/video/{THUMBNAIL_ID_HERE}_1920x1080.jpg?r=pad",
                    "link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F{THUMBNAIL_ID_HERE}_1920x1080.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
                }
            ],
            "resource_key": "{RESOURCE_KEY}"
        }
        //More info here
  }

要检索您正在寻找的信息,您需要执行以下操作:

//Check to see if the request didn't fail
if($response["status"] === 200){

  //Get the pictures object
  $thumbnailsObject = $response["body"]["data"]["pictures"];

  //Get the URI, so next time you can directly request the thumbnails instead of going through the whole video data
  $thumbnailUri = $thumbnailsObject["uri"];

  //Array of thumbnail links that'll be populated after loop.
  $thumbnails = [];
  //thumbnail without S
  foreach($thumbnailsObject["sizes"] as $thumbnailObject){
    $thumbnails[] = $thumbnailObject["link"];
  }

  //Done, do something with $thumbnails
}

仅请求缩略图

获得URI后,您可以在以后的请求中调用缩略图,如下所示:

GET https://api.vimeo.com/videos/{video_id}/pictures/{picture_id}

$response = $vimeoLib->request("/me/videos/".$vimeo_id."/pictures/".$picture_id);

或使用之前的$thumbnailUri变量。

$response = $vimeoLib->request("/me".$thumbnailUri);

答案 1 :(得分:1)

我的私人视频缩略图也有同样的问题。为了获取私人视频缩略图,我下载了Vimeo SDK并使用了此功能。

public function get_video_thumbnail_by_id($video_id, $thumbType = 'medium')
{
    try {
        if (empty($video_id)) {
            throw new Exception("Invalid video ID");
        }

        $response = $this->client->request("/me/videos/$video_id");

        if ($response["status"] === 200) {
            $response_pictures = $response["body"]["pictures"]['sizes'];

            switch ($thumbType) {
                case 'small':
                    $result = $response_pictures[2]['link'];
                    break;
                case 'medium':
                    $result = $response_pictures[3]['link'];
                    break;
                case 'large':
                    $result = $response_pictures[4]['link'];
                    break;
                default:
                    $result = $response_pictures[3]['link'];
                    break;
            }
        } else {
            $result = null;
        }

        return $result;
    } catch (VimeoUploadException $ex) {
        return null;
    }
}