iframe嵌入带有php变量作为源的视频不起作用

时间:2019-03-29 20:39:11

标签: php

我有一个存储数组的变量,在该数组中有一个YouTube网址。

当我var dump时

$node->field_video_link

如下所示:

array(1) { ["und"]=> array(1) { [0]=> array(5) { ["video_url"]=> string(43) "https://www.youtube.com/watch?v=SC9BgH_L29c" ["thumbnail_path"]=> string(61) "public://video_embed_field_thumbnails/youtube/SC9BgH_L29c.jpg" ["video_data"]=> string(34) "a:1:{s:7:"handler";s:7:"youtube";}" ["embed_code"]=> NULL ["description"]=> NULL } } }

现在,我想将视频链接用作iframe,以便用户可以观看网站,而我的操作如下:

$sample1_video_link = (isset($node->field_video_link['und'])) ? file_create_url($node->field_video_link['und'][0]['video_url']) : '';

<iframe width="420" height="315"
src="<?php print $sample1_video_link; ?>">
</iframe>

当我查看页面时,视频缩略图不会出现,而是看到一条消息,指出“ www.youtube.com拒绝建立连接”

但是,如果我从youtube获取随机网址,则iframe可以正常工作。

错误如下图所示:

enter image description here

我可能做错了什么吗?

已编辑:

我现在使用echo而不是print,并且当我重新加载页面时,仍然是相同的错误。

<iframe src="<?php echo $sample1_video_link; ?>"></iframe>

检查页面时,我看到以下内容:

<iframe src="https://www.youtube.com/watch?v=SC9BgH_L29c"></iframe>

显然是存储在数组中的值。

1 个答案:

答案 0 :(得分:1)

使用echo而不是print

src="<?php echo $sample1_video_link; ?>">

YouTube也使用该网址生成嵌入网址(在视频下方共享),而是使用其生成iframe示例结构:

<iframe width="560" height="315" src="https://www.youtube.com/embed/SC9BgH_L29c" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

按照您的代码,您可以将视频ID添加到“ https://www.youtube.com/embed/”之后

src="<?php echo str_replace("https://www.youtube.com/watch?v=","https://www.youtube.com/embed/",$sample1_video_link; ?>">