YouTube Embed, URL From Database Query - php

I want to display a youtube embed using the URL that the user put on their profile.
It works correctly if the URL in the database is http://youtube.com/embed/URL
I'd like users to be able to just input a regular URL (http://youtube.com/URL because I want it to be easy for them.
Is there a way to get around this?
Thanks a lot for any help, I'm quite new at this!

Try copying and pasting this, and see if it works:
echo '<br>';
echo '<iframe width="560" height="315" src="' . stripslashes($row['videourl']) . '" frameborder="0" allowfullscreen></iframe>';
If it's not showing up based on the fact that the person entering the URL doesn't place embed in their link, then what I would recommend is just having the person enter the ID of the video, and you create the rest of the URL.
For example, if the URL to the video a user wants to post is https://www.youtube.com/watch?v=0TL5CSPFzTU then they would just enter 0TL5CSPFzTU and you would output your code as such:
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . stripslashes($row['videourl']) . '" frameborder="0" allowfullscreen></iframe>';
You'd have to put something in place that stops them from entering the full URL, or anything more than the ID, but there are plenty of tutorials out there for accomplishing that.

Related

How to play youtube video from MySQL database

I have a form which a user can enter a youtube link along with their product. I want to be able to play that video (which it's link is stored in a MySQL database, the problem is that the <iframe> tag needs a youtube "Embed" link and not the URL, is there a way to convert the URL to the Embed link?
video_url = https://www.youtube.com/watch?v=1DIDWWKk8Bg
//code to connect to MySQL DB and get all rows
$videourl = $row["video_url"];
}
?>
<iframe width="420" height="345" src="<?php $videourl?>">
</iframe>
Regular YouTube link look like this
https://www.youtube.com/watch?v=1234
Embed YouTube link look like this
https://www.youtube.com/embed/1234
You need just to replace this watch?v= to embed/
$link = "https://www.youtube.com/watch?v=1234";
echo str_replace("watch?v=", "embed/",$link);
You have to use embed.
like this.
$videourl = explode("https://www.youtube.com/watch?v=", $row['videourl']);
<iframe class="iframeVideo" src="https://www.youtube.com/embed/' . $videourl[1] . '"></iframe>
$videourl has to look like this https://www.youtube.com/watch?v=3J6o7hcm8bE
I hope this works for you.
You cannot not use the exact url from the youtube video. The url used in iframes has the following format http://www.youtube.com/embed/unique_id_here. So you only have to save the unique_id for each video. It is only a part of the url. For example, from this url: https://www.youtube.com/watch?v=hdhjs88737&list=RDEMnLv8DM0l6F9uGd8jrENGgg&index=3, the unique id is the one in bold: hdhjs88737. So here is what I would do. Go to any video on youtube and click the share option, under the options select embed. Then copy the code. It will look like this:
<iframe width="560" height="315" src="https://www.youtube.com/embed/<unique_id_here>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Modify the parameters in the iframe to fit your preferences then only save the unique_id to your database. That should solve your problem. Check this API reference for a detailed guide.
Here is what I used, I'm not sure if this code should be there twice but it works so...
<?php
str_replace("watch?v=", "embed/",$videourl);
?>
<iframe width="520" height="345" src="<?php echo str_replace("watch?v=", "embed/",$videourl);?>">
</iframe>
Thank you for the help.

PHP: Using google map embed iframe and pulling a postcode from the database to display

http://www.youtube.com/watch?v=HTm-3Cduafw
Above is basically what I am after but I have tried everything to get it working...I want to pull the postcode from the database and use that, instead of a full address. Its so each user has a different map position, based on what postcode is stored in their account, in the database.
Im using dreamweaver, I have brought in the recordset and the php bit that I am trying to use is below.
<?php echo "$postcode";?>
Above is the snippet and below is what i have so far
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=<?php echo "$postcode";?>&aq=&t=h&g=<?php echo "$postcode";?>&ie=UTF8&hq=&hnear=<?php echo "$postcode";?>&z=13&output=embed"></iframe>
Thanks guys!
Just use & instead of &, because $amp; is the html tag

Difficulty including wordpress shortlink in am embedded (iframe) tweet button

I am trying to programmatically include a tweet button, in an iframe, on a page. I want this to tweet the wordpress shortlink of a specific post.
I am using PHP to insert into my wordpress database the following HTML iframe code to include the weet button. The code looks like this
'<iframe allowtransparency="true" frameborder="0" scrolling="no"
src="https://platform.twitter.com/widgets/tweet_button.html
?count=horizonatal?url=http://www.mywebsite.com?p= '
. $post_id . '"></iframe>'
As you can see I am concatenating the $post_id, which is in the PHP code, with the rest of the wordpress shortlink "http://www.mywebsite.com?p=". The problem appears to be that the twitter widget interprets the '?' in my url attribute as a query for itself -- as opposed to a query for my wordpress site - and what happens is I can only get http://www.mywebsite.com tweeted.
Does anyone know a way around this?
You need use urlencode:
'<iframe allowtransparency="true" frameborder="0" scrolling="no"
src="https://platform.twitter.com/widgets/tweet_button.html
?count=horizonatal?url='.urlencode('http://www.mywebsite.com?p='.$post_id)
.'"></iframe>'
Reading: http://www.php.net/urlencode

How to get around This Content cannot be displayed in a frame? (Youtube issue)

I'm trying to keep a database of posted videos on a site I am currently developing. To be able to do this I have to store the URLs of the videos into a database and then I call the url from the database using php and sql by doing this bare in mind I am using this from a php defined area on my homepage. If I use this code it works on other video post sites!
echo '<iframe width="640" height="390" src="', $row['Post_URL'],'"frameborder="0" allowFullScreen></iframe>';
This will not work it says content cannot be displayed in a frame but if I do this it works.
<?php
echo '<iframe width="560" height="315" src="http://www.youtube.com/embed/wardQ7W8hPU" frameborder="0" allowfullscreen></iframe>';
?>
why does the one work properly and the other not? what is a way around this so I can call from the database into php and echo it out so it will display properly on the page?
I got the same problem today. As stated in one of the comments, the problem is the url used...
The iframe embed only works with urls looking like:
http://www.youtube.com/embed/[video_id]
Most of people are using the "share" feature, that generates minified urls like:
http://youtu.be/[video_id]
However, if you look into the "embed" feature, you'll notice that it generates an iframe with src urls like the first example.
So to answer the question, when using iframes to embed, use the src url like the first example.
Try this
<iframe width="640" height="390" src="<?php echo $row['Post_URL'] ?>" frameborder="0" allowFullScreen></iframe>
OR this
<?php
echo "<iframe width='640' height='390' src='{$row['Post_URL']}' frameborder='0' allowFullScreen></iframe>";
?>
OR this
<?php
echo "<iframe width='640' height='390' src='" . $row['Post_URL'] . "' frameborder='0' allowFullScreen></iframe>";
?>
they all are the same and will do the same process.
I'm doing this with dynamic data and it's easier for the client to use the youtube Share link.
So... it's then just a matter of getting the youtube video ID from that url.
// we can't use the "share" link so need to just get the video ID
$youtubePath = parse_url($youtubeShareLink,PHP_URL_PATH);
$youtubeId = str_replace('/','',$youtubePath);
and now we can use the ID in an iframe (adjust width and height per embed options in youtube)
<iframe width="560" height="315" src="//www.youtube.com/embed/<? echo $youtubeId; ?>?rel=0" frameborder="0" allowfullscreen></iframe>

Fetch user id from Soundcloud embed code

I need to embed soundcloud url in my website , SO I am asking site users to submit the SOUNDCLOUD URL they wish to embed on their page
Is there any way I can get using PHP
1. value of url in the iframe within src
2. and the user id i.e. 822654
<iframe width="100%" height="450" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fusers%2F847884 &show_artwork=true"></iframe>
I tried parse_url(PHP function) but failed to fetch what I want
If someone , can help me also know that whether every user gets an id like 847884 (in case above) or the embed code may have vanity url(like instead of 847884 can user have /bob/ or /andrew/) ?
Script:
<?php
$subject = "<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fusers%2F847884&show_artwork=true\"></iframe>";
$pattern = '/<iframe[^>]*\ssrc="[^"]*\?url=([^&]*%2Fusers%2F(\d+)[^&]*)&/';
preg_match($pattern, $subject, $matches);
echo urldecode($matches[1]), "\n";
echo $matches[2], "\n";
?>
Output:
http://api.soundcloud.com/users/847884
847884
See it and test it here.

Categories