Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查youtube缩略图是否存在_Php_Youtube - Fatal编程技术网

Php 检查youtube缩略图是否存在

Php 检查youtube缩略图是否存在,php,youtube,Php,Youtube,如何知道在不使用谷歌API的情况下是否存在最大分辨率的youtube缩略图 例如,带有代码的视频没有maxresolution。如果我们打开URL:http://i1.ytimg.com/vi/4rlR7EncGD0/maxresdefault.jpg我们可以看到灰色矩形,这意味着没有缩略图,对吗 我的代码,只是检查文件是否存在。如果文件存在,则会显示在网站中。毫无疑问,这对那些“灰色矩形”不起作用。我用的是: $item = '4rlR7EncGD0'; $Ma

如何知道在不使用谷歌API的情况下是否存在最大分辨率的youtube缩略图

例如,带有代码的视频没有maxresolution。如果我们打开URL:
http://i1.ytimg.com/vi/4rlR7EncGD0/maxresdefault.jpg
我们可以看到灰色矩形,这意味着没有缩略图,对吗

我的代码,只是检查文件是否存在。如果文件存在,则会显示在网站中。毫无疑问,这对那些“灰色矩形”不起作用。

我用的是:

        $item = '4rlR7EncGD0';
        $MaxResURL = 'https://i1.ytimg.com/vi/'.$item.'/maxresdefault.jpg';
        //print $MaxResURL;

        $curl = curl_init();
        curl_setopt_array($curl, array(    
        CURLOPT_URL => $MaxResURL,
        CURLOPT_HEADER => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_NOBODY => true));

        $header = explode("\n", curl_exec($curl));
        curl_close($curl);

        //var_dump($header);        

        //If maxres image exists do something with it.
        if (strpos($header[0], '200') !== false) {    
            print $MaxResURL;
        }else{
            print 'Some Other IMG';
        }
这将使用curl检查标题。如果“200”可以打印url,当然你可以显示图片或者你特别需要做的事情,我用它来添加facebook和twitter元标记

我使用了以下答案中的代码:

检查标题上的200

要获取标题,因为我使用的是wordpress,而且出于某种原因,get_headers()不起作用

我不是专家,我不知道这是否是解决此问题的最佳方法,但此代码对我有效,希望它能有所帮助