我使用此功能从YouTube图像URL获取二进制数据。但是,将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
在上面的情况下,将获得https://www.googleapis.com/youtube/v3/videos?part=snippet&Amp;Id=s4bw0hqotfu&amkemek;
然后您处理结果以获取可用的缩略图:
"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
}
},