我尝试检查是否不存在maxres,请显示标准等等,但如果找不到maxres,则找不到任何URL,为什么?
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["maxres"]["url"];
if($thumbnails == ""){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["standard"]["url"];
}elseif($thumbnails == ""){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["high"]["url"];
}elseif($thumbnails == ""){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["medium"]["url"];
}elseif($thumbnails == ""){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["default"]["url"];
}
所以我注意到您的脚本的几个问题是您的主要问题是您的if语句将输入第一个循环,如果失败将永远不会输入另一个循环
我已经在服务器上测试了以下脚本,发现它正在工作,我希望您对您很清楚,如果不是,我留下了评论
<?php
function GetData($url,$post){
$ch = curl_init($url . $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$res = GetData("https://www.googleapis.com/youtube/v3/videos?","part=snippet&id=<video_id>&key=<your_key>");
$result = json_decode($res);
//Check for maxres thumb
$maxresThumb = $result->items[0]->snippet->thumbnails->maxres->url;
$thumbnail = '';
if($maxresThumb == '' || $maxresThumb == null){
//if maxresThumb is not existent check for high res
$highThumb = $result->items[0]->snippet->thumbnails->high->url;
if($highThumb == '' || $highThumb == null){
//Check for standard thumb
$standardThumb = $result->items[0]->snippet->thumbnails->standard->url;
if($standardThumb == '' || $standardThumb == null){
//CHeck for next thumb....
}else{
$thumbnail = $standardThumb;
}
}else{
$thumbnail = $highThumb;
}
}else{
$thumbnail = $maxResThumb;
}
?>
现在正在工作,我更改为
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["maxres"]["url"];
if($thumbnails == "" || $thumbnails == null){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["standard"]["url"];
if($thumbnails == "" || $thumbnails == null){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["high"]["url"];
if($thumbnails == "" || $thumbnails == null){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["medium"]["url"];
if($thumbnails == "" || $thumbnails == null){
$thumbnails = $vid["items"][0]["snippet"]["thumbnails"]["default"]["url"];
}
}
}
}