api - 如何从youtube图像URL将图像上传到我的Thumb文件夹

标签 api youtube youtube-api

我使用此功能从youtube图片网址获取二进制数据。但是将api v2更改为api v3后,我过去使用的功能不再起作用。该函数未返回任何值。请帮助我解决这个问题。

<?php

$thumbnail_link = 'https://i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg';


if ( function_exists('curl_init') ) 
    {

        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $thumbnail_link);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        // Getting binary data
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        $image = curl_exec($ch);
        curl_close($ch);

        echo $image; //this will return an empty data why is it?



        //  create & save image;
        $img_res = @imagecreatefromstring($image);
        if($img_res === false)
            return FALSE;

        $img_width = imagesx($img_res);
        $img_height = imagesy($img_res);

        $resource = @imagecreatetruecolor($img_width, $img_height);

        if( function_exists('imageantialias'))
        {
            @imageantialias($resource, true); 
        }

        @imagecopyresampled($resource, $img_res, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
        @imagedestroy($img_res);

        switch($ext)
        {
            case ".gif":
                //GIF
                @imagegif($resource, $upload_path . $thumb_name);
            break;
            case ".jpg":
                //JPG
                @imagejpeg($resource, $upload_path . $thumb_name);
            break;  
            case ".png":
                //PNG
                @imagepng($resource, $upload_path . $thumb_name);
            break;
        }


    }

?>

最佳答案

您的缩略图链接i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg不存在。

您可以使用v3 API获取正确的缩略图网址:
https://developers.google.com/youtube/v3/docs/videos/list

在上述情况下,它将是GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key= {YOUR_API_KEY}

然后,您处理结果以获取可用的缩略图:

"thumbnails": {
 "default": {
  "url": "https://i.ytimg.com/vi/s4bw0HQotfU/default.jpg",
  "width": 120,
  "height": 90
 },
 "medium": {
  "url": "https://i.ytimg.com/vi/s4bw0HQotfU/mqdefault.jpg",
  "width": 320,
  "height": 180
 },
 "high": {
  "url": "https://i.ytimg.com/vi/s4bw0HQotfU/hqdefault.jpg",
  "width": 480,
  "height": 360
 },
 "standard": {
  "url": "https://i.ytimg.com/vi/s4bw0HQotfU/sddefault.jpg",
  "width": 640,
  "height": 480
 },
 "maxres": {
  "url": "https://i.ytimg.com/vi/s4bw0HQotfU/maxresdefault.jpg",
  "width": 1280,
  "height": 720
 }
},

关于api - 如何从youtube图像URL将图像上传到我的Thumb文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30217920/

相关文章:

javascript - YouTube视频结束时隐藏Div

android - YouTube 数据 API 未启用 Alpha 测试,但已启用开发模式

javascript - youtube restapi (v3) 集成在 titanium 中用于视频上传

java - Java中创建API库的方法是什么

c# - 在 C# 中将 JSON 数组反序列化为对象

javascript - 是否可以从嵌入式YouTube iframe获取<video>元素?

youtube-api - startindex >= 100 时的空响应

javascript - Google API - 未找到文件 ID - 但返回的数据对应于我输入的folderId

java - LinkedIn 职位发布 API 在 Java 中的实现

android - 如何使用YouTube API V3从Android应用上传视频?