i use this function to get binary data from youtube image url. but after changing the api v2 into api v3 the function i have used in past not longer work. the function did not return any values. please help me to solve this problem.
我使用此函数从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;
}
}
?>
1 个解决方案
#1
your thumbnail link i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg does not exist.
您的缩略图链接i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg不存在。
You can use the v3 API to get the correct thumbnail urls: https://developers.google.com/youtube/v3/docs/videos/list
您可以使用v3 API获取正确的缩略图网址:https://developers.google.com/youtube/v3/docs/videos/list
In the above case it would be GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key={YOUR_API_KEY}
在上面的例子中,它将是GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key={YOUR_API_KEY}
Then you process the results to get the available thumbnails:
然后处理结果以获取可用的缩略图:
"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
}
},
#1
your thumbnail link i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg does not exist.
您的缩略图链接i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg不存在。
You can use the v3 API to get the correct thumbnail urls: https://developers.google.com/youtube/v3/docs/videos/list
您可以使用v3 API获取正确的缩略图网址:https://developers.google.com/youtube/v3/docs/videos/list
In the above case it would be GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key={YOUR_API_KEY}
在上面的例子中,它将是GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=s4bw0HQotfU&key={YOUR_API_KEY}
Then you process the results to get the available thumbnails:
然后处理结果以获取可用的缩略图:
"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
}
},