如何查看缩略图YouTube API

时间:2019-03-20 20:11:04

标签: php youtube-api

我尝试检查maxres是否不存在,然后显示标准等,但是如果找不到maxres,则不添加任何网址,为什么?

$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"];
  }

2 个答案:

答案 0 :(得分:1)

我在您的脚本中注意到了几个问题,主要问题是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;
}
?>

答案 1 :(得分:0)

现在可以使用了,我更改为

$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"];
        }
      }
    }
  }