Fetch user id from Soundcloud embed code - php

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.

Related

How to get url of video element of an iframe using php

Good afternoon friend,
I am trying to be able to take the video from the url of the iframe, from what I see I have to first click the video so that the video element is visible in html.
Isn't there a way to automate the process? Click automatically when entering the url and extract the url from the video
The url is: https://feurl.com/v/2625zf2pddy2ge
I'm supposed to get the following url from the video at the end.
https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0
My code is:
<?php
$data = file_get_contents('https://feurl.com/v/2625zf2pddy2ge');
preg_match('/<video[^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/i', $data, $matches);
$video = $matches[1];
echo $video;
?>
Both links dont work but assuming html looks something like this a.html:
<video width="320" height="240" src="https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0"></video>
Try this:
<?php
$data = file_get_contents('a.html');
preg_match('/<video.*?src="(.*?)"/', $data, $matches);
var_dump($matches[1]);
Output:
string(279) "https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0"
If it doesnt work dump your $data and make sure the video tag is even there. Sometimes this is loaded later via ajax, so it is not even available in initial response.

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.

youtube embed code with PHP

I have created a blog in php. Users can write something and post it. If this includes a web address then a link automated is created using this :
<?php
//...code
$row['comment'] = preg_replace('#(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\#!$&\'()*+,;=%]*)?)?)#', '<font color="#69aa35">$1</font>', $row['comment']);
?>
Using this in posts, text posted successfuly and web address displayed in a link format inside text. Any idea how can I change this, so that if there is a link of youtube, then a youtube frame to be created. For example in facebook, when you post a youtube address, a youtube frame created and posted instead of a link.
I have improved my answer and tested this code:
<?php
// This is your comment string containing the youtube link
$string="Here is a link - https://www.youtube.com/watch?v=LJHFXenOPi4";
// This will remove all links from the input string
preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:#=_#&%~,+$]+/', $string, $matches);
foreach($matches as $url){
// Parse each url within the comment data
$input = parse_url($url);
if ($input['host'] == 'youtube.com' || $input['host'] == 'www.youtube.com' ) {
// If it is a youtube link, then parse the get variables
parse_str(parse_url($url, PHP_URL_QUERY), $variables);
// Echo out the iframe with the relevant video ID
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$variables['v'].'" frameborder="0" allowfullscreen></iframe>';
}
}
?>
I hope this is what you were looking for, it has worked for me on a few tests
You know the solution don't you? :) If the snippet contains youtube.com URL, then using the same pattern matching you can replace it with a youtube embed tag :)
Basically it will be something like this in pseudo-code.
Check the comment pasted to see if youtube URL present (modify the same regex you are using for finding URL, just make it specific for youtube)
If yes, then replace it with:
<iframe type="text/html"
width="640"
height="385"
src="<youtube URL>"
frameborder="0">
</iframe>

YouTube Embed, URL From Database Query

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.

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>

Categories