【问题标题】:How to display a video in an iframe on page in Laravel?如何在 Laravel 页面的 iframe 中显示视频?
【发布时间】:2018-02-01 21:31:01
【问题描述】:

您好,我正在尝试在页面上显示来自 YouTube 的 iframe 视频。运行页面时,我在空白块上收到 404 错误。可能有什么问题?

我的代码是:

<iframe width="854" height="480" src="{{ $vid->videoURL . '&output=embed' }}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

【问题讨论】:

  • 网址有效,复制粘贴
  • 在浏览器中检查您的控制台/网络选项卡。
  • Refused to display 'https://www.youtube.com/watch?v=8nbsSkwSbb0&amp;output=embed' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
  • 将您的网址设为https://www.youtube.com/embed/8nbsSkwSbb0
  • {{ 'https://www.youtube.com/embed/' . explode('?v=', $vid-&gt;videoURL)[1] }}

标签: php html laravel iframe


【解决方案1】:

将您的 urlhttps://www.youtube.com/watch?v=8nbsSkwSbb0&amp;output=embed 更改为 https://www.youtube.com/embed/8nbsSkwSbb0/embed 端点允许外部请求,而/watch 不允许。我认为,您可以像这样在 Video 模型中创建 accessor 方法:

public function getSrcAttribute()
{
    // $this->videoUrl: https://www.youtube.com/watch?v=8nbsSkwSbb0
    parse_str(parse_url($this->videoURL)['query'], $output);
    return 'https://www.youtube.com/embed/' . $output['v'];
}

然后像这样使用它:

<iframe
src="{{ $vid->src }}"
width="854"
height="480"
frameborder="0"
allow="autoplay; encrypted-media"
allowfullscreen></iframe>

【讨论】:

    猜你喜欢
    • 2022-08-10
    • 2016-08-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多