1

I have a variable that stores an array, and in that array there is a YouTube url.

When I var dump

$node->field_video_link

It looks like the following:

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 } } }

Now, I want to use the video link as an iframe so that users can watch through the site and I did it like the following:

$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>

When I go view the page, the video thumbnail does not appear, and instead, I see a message that says "www.youtube.com refused to establish connection"

However, if I grab a random URL from youtube, the iframe works.

The error looks like in the below image:

enter image description here

Anything I am possibly doing wrong?

EDITED:

I now use echo instead of print and when I reload the page, still the same error.

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

When I inspect the page, I see the following:

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

Which is apparently the same value being stored in the array.

1
  • What does file_create_url do? I'm not familiar with that function Commented Mar 29, 2019 at 20:42

1 Answer 1

1

use echo instead of print:

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

also youtube generates embedding url (share below the video) use that one instead they also generate an example iframe structure :

<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>

going by your code you can jsut add the video id after "https://www.youtube.com/embed/"

or

src="<?php echo str_replace("https://www.youtube.com/watch?v=","https://www.youtube.com/embed/",$sample1_video_link; ?>">
Sign up to request clarification or add additional context in comments.

10 Comments

Or the shorthand version <?= $sample1_video_link ?>
I still see exactly the same error if I echo the variable
youtube generates embedding links maybe this helps : youtube.com/embed/SC9BgH_L29c
I added to my answer as well
I cannot use the url directly in the html. It needs to come from the variable as it is because its dynamic content.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.