【问题标题】:YouTube videos don’t playYouTube 视频无法播放
【发布时间】:2015-04-17 17:57:03
【问题描述】:

谢谢!!!!但这并没有帮助......听着我是这个蚂蚁的新手我不知道任何关于 PHP 的想法我有购买这个脚本,我向开发人员寻求帮助,但他们没有回答我。我有一个名为(Youtube.class.php)的文件,我相信这是我的问题所在。我的网站是(madolino.com),如果你愿意,如果你有时间的话,我会把它下面的(Youtube.class.php)发给你,也许可以帮助你…… ..

<?php
# YouTube PHP class
# used for embedding videos as well as video screenies on web page without single line of HTML code
#
# Dedicated to my beloved brother FILIP. Rest in peace!
#
# by Avram, www.avramovic.info
class Youtube
{
    private $id = NULL;
    /**
     * Set YouTube ID explicitly
     *
     * This method sets YouTube ID explicitly. It checks if the ID is in good format. If yes it will set it
     * and return true, and if not - it will return false
     *
     * @access public
     * @param string $id YouTube ID
     * @return boolean Whether the ID has been set successfully
     */
    public function setID($id)
    {
        if (preg_match('/([A-Za-z0-9_-]+)/', $url, $matches)) {
            $this->id = $id;
            return true;
        } else
            return false;
    }
    /**
     * Get string representation of YouTube ID
     *
     * This method returns YouTube video ID if any. Otherwise returns null.
     *
     * @access public
     * @return string YouTube video ID if any, otherwise null
     */
    public function getID()
    {
        return $this->id;
    }
    public function url2id($url)
    {
        $aux  = explode("?", $url);
        $aux2 = explode("&", $aux[1]);
        foreach ($aux2 as $campo => $valor) {
            $aux3 = explode("=", $valor);
            if ($aux3[0] == 'v')
                $video = $aux3[1];
        }
        return $this->id = $video;
    }
    /**
     * Parse YouTube URL and return video ID.
     *
     * This method sreturnns YouTube video ID if any. Otherwise returns null.
     *
     * @access public
     * @static
     * @param string $url URL of YouTube video in any of most commonly used forms
     * @return string YouTube video ID if any, otherwise null
     */
    public static function parseURL($url)
    {
        if (preg_match('/watch\?v\=([A-Za-z0-9_-]+)/', $url, $matches))
            return $matches[1];
        else
            return false;
    }
    /**
     * Get YouTube video HTML embed code
     *
     * This method returns HTML code which is used to embed YouTube video in page
     *
     * @access public
     * @param string $url YouTube video URL. If this cannot be parsed it will be used as video ID. It can be omitted
     * @param integer $width Width of embedded video, in pixels. Defaults to 425
     * @param integer $height Height of embedded video, in pixels. Defaults to 344
     * @return string HTML code which is used to embed YouTube video in page
     */
    public function embedVideo($url = null, $width = 425, $height = 344)
    {
        if ($url == null)
            $videoid = $this->id;
        else {
            $videoid = YouTube::parseURL($url);
            if (!$videoid)
                $videoid = $url;
        }
        return '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="http://www.youtube.com/v/' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"><embed src="http://www.youtube.com/v/' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1" allowfullscreen="true" type="application/x-shockwave-flash" wmode="transparent" width="' . $width . '" height="' . $height . '"></embed></object>';
    }
    /**
     * Get URL of YouTube video screenshot
     *
     * This method returns URL of YouTube video screenshot. It can get one of three screenshots defined by YouTube
     *
     * @access public
     * @param string $url YouTube video URL. If this cannot be parsed it will be used as video ID. It can be omitted
     * @param integer $imgid Number of screenshot to be returned. It can be 1, 2 or 3
     * @return string URL of YouTube video screenshot
     */
    public function getImgURL($url = null, $imgid = 1)
    {
        if ($url == null)
            $videoid = $this->id;
        else {
            $videoid = YouTube::parseURL($url);
            if (!$videoid)
                $videoid = $url;
        }
        return "http://img.youtube.com/vi/$videoid/$imgid.jpg";
    }
    /**
     * Get URL of YouTube video screenshot
     *
     * This method returns URL of YouTube video screenshot. It can get one of three screenshots defined by YouTube
     * DEPRECATED! Use getImgURL instead.
     *
     * @deprecated
     * @see getImgURL
     * @access public
     * @param string $url YouTube video URL. If this cannot be parsed it will be used as video ID. It can be omitted
     * @param integer $imgid Number of screenshot to be returned. It can be 1, 2 or 3
     * @return string URL of YouTube video screenshot
     */
    public function getImg($url = null, $imgid = 1)
    {
        return getImgURL($url, $imgid);
    }
    /**
     * Get YouTube screenshot HTML embed code
     *
     * This method returns HTML code which is used to embed YouTube video screenshot in page
     *
     * @access public
     * @param string $url YouTube video URL. If this cannot be parsed it will be used as video ID
     * @param integer $imgid Number of screenshot to be returned. It can be 1, 2 or 3
     * @param string $alt Alternate text of the screenshot
     * @return string HTML code which embeds YouTube video screenshot
     */
    public function showImg($url = null, $imgid = 1, $alt = 'Video screenshot')
    {
        return "<img src='" . $this->getImgURL($url, $imgid) . "' width='130' height='97' border='0' alt='" . $alt . "' title='" . $alt . "' />";
    }
    public function search($query, $max = 5)
    {
        if ($_SERVER['HTTP_X_FORWARD_FOR']) {
            $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $feedURL = 'http://gdata.youtube.com/feeds/base/videos?q=' . $query . '&client=ytapi-youtube-search&format=5&restriction=' . $ip . '&max-results=' . $max . '&v=2';
        $sxml    = simplexml_load_file($feedURL);
        $i       = 0;
        foreach ($sxml->entry as $entry) {
            $details                 = $entry->content;
            $info[$i]["title"]       = $entry->title;
            $aux                     = explode($info[$i]["title"], $details);
            $aux2                    = explode("<a", $aux[0]);
            $aux3                    = explode('href="', $aux2[1]);
            $aux4                    = explode('&', $aux3[1]);
            $info[$i]["link"]        = $aux4[0];
            $details_notags          = strip_tags($details);
            $texto                   = explode("From", $details_notags);
            $info[$i]["description"] = $texto[0];
            $aux                     = explode("Views:", $texto[1]);
            $aux2                    = explode(" ", $aux[1]);
            $info[$i]["views"]       = $aux2[0];
            $aux                     = explode("Time:", $texto[1]);
            $aux2                    = explode("More", $aux[1]);
            $info[$i]["time"]        = $aux2[0];
            $imgs                    = strip_tags($details, '<img>');
            $aux                     = explode("<img", $imgs);
            array_shift($aux);
            array_shift($aux);
            $aux2 = explode("gif\">", $aux[4]);
            array_pop($aux);
            $aux3   = $aux2[0] . 'gif">';
            $aux[]  = $aux3;
            $images = '';
            foreach ($aux as $campo => $valor) {
                $images .= '<img' . $valor;
            }
            $info[$i]["images"] = $images;
            $i++;
        }
        return $info;
    }
    public function linkSearch($query, $max = 5)
    {
        if ($_SERVER['HTTP_X_FORWARD_FOR']) {
            $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $feedURL = 'http://gdata.youtube.com/feeds/base/videos?q=' . rawurlencode($query) . '&client=ytapi-youtube-search&format=5&restriction=' . $ip . '&max-results=' . $max . '&v=2';

        $sxml    = simplexml_load_file($feedURL);
        $i       = 0;
        foreach ($sxml->entry as $entry) {
            $details  = $entry->content;
            $aux      = explode($entry->title, $details);
            $aux2     = explode("<a", $aux[0]);
            $aux3     = explode('href="', $aux2[1]);
            $aux4     = explode('&', $aux3[1]);
            $info[$i] = $aux4[0];
            $i++;
        }
        return $info;
    }

}

【问题讨论】:

    标签: object iframe


    【解决方案1】:

    转到您要嵌入的视频,然后找到这个...

    复制该代码并将其粘贴到您希望显示视频的位置。

    代码...

    echo '<iframe width="'.$width.'" height="'.$height.'" src="https://www.youtube.com/embed/'.$videoid.'?rel=0&fs=1&loop=0&autoplay=1" frameborder="0" allowfullscreen></iframe>';
    

    【讨论】:

    • 感谢您的快速回复。我的网站是自动抓取视频并使用此代码.... youtube.com/v' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1"> youtube.com/v' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1" allowfullscreen="true" type="application/x-shockwave-flash" wmode="transparent " width="' . $width . '" height="' . $height . '">
    • 我已经更新了我上面的问题,因为我无法将代码粘贴到 cmets 中
    【解决方案2】:

    好的,再次查看您的代码可能会有所帮助。

    要使用 YouTube.class 在页面中显示视频,试试这个。

    <?php
    echo '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="youtube.com/v/' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"><embed src="youtube.com/v/' . $videoid . '?rel=0&fs=1&loop=0&autoplay=1" allowfullscreen="true" type="application/x-shockwave-flash" wmode="transparent" width="' . $width . '" height="' . $height . '"></embed></object>';
    ?>
    

    只要您正确设置了正确的变量($width、$height、$videoid),这应该可以工作。

    【讨论】:

    • 谢谢克里斯!!!但是您提供的代码没有帮助大多数视频不在我的网站上播放,但它们在 YouTube 上播放。我已经发布了大厅 Youtube.class.php 但我不确定是否存在问题,所以如果有人可以给我,我仍然需要帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-07-13
    • 2021-05-29
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多