I'm working with a WordPress plugin that outputs a text link, but the user inputs the link from an 3rd party site. the links are youtu.be format, I need the video's ID. I have tried this method but I'm not getting it so far
original code:
if($video_link != "") {
echo '<p>';
echo 'Please visit : Multimedia link for more photos and information' ;
echo '</p>';
}
Output link :
http://youtu.be/abcdefghijkl
tried this, can't get it to work:
if($video_link != "")
{
$url = $_GET['url'];
$video_id = substr( parse_url($url, PHP_URL_PATH), 1 );
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed'.$video_id.'" frameborder="0" allowfullscreen></iframe>' ;
}
Output Iframe does not work :
EDIT: Ignore my previous answer, your parse works fine. You just forgot the / after 'embed' in your iframe src attribute.
Or if you want, you can just remove the substr part in your parse (which is what strips the slash)
$video_id = parse_url($url, PHP_URL_PATH);
Related
Hey i have problem i need from one json get information to another json link.
I need get all(maxResults) videoId from this link below
https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCynfZM0Edr9cA4pDymb2rEA&maxResults=20&key=AIzaSyDVTF2abNVa5pRitb8MVz1ceJFhE-2y_qk
to this link below in this area [NEED HERE videoId with , each]
https://www.googleapis.com/youtube/v3/videos/?id=[NEED HERE videoId with , each]&part=statistics&key=AIzaSyDVTF2abNVa5pRitb8MVz1ceJFhE-2y_qk
All code
<?php
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCynfZM0Edr9cA4pDymb2rEA&maxResults=20&key=AIzaSyDVTF2abNVa5pRitb8MVz1ceJFhE-2y_qk'));
$url = 'https://www.googleapis.com/youtube/v3/videos/?id=[NEED HERE IDS]&part=statistics&key='.$API.'';
$videoViews = json_decode(file_get_contents($url));
foreach($videoList->items as $item){
//Shows embed videos from channels
if(isset($item->id->videoId)){
echo '<div class="video">
<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
<h5>'. $item->snippet->title .'</h5>
</div>';
}
}
?>
i need look like this 3 videoId added but from first json information
https://www.googleapis.com/youtube/v3/videos/?id=w_TLR7K_g98,H5_LI7caH1M,ishpS2v9uvo&part=statistics&key='.$API.'
Loop through all the items, and put the video IDs into an array. Then use implode() to combine them into a comma-separated string.
$videoIDs = array();
foreach ($videoList->items as $item) {
if(isset($item->id->videoId)){
$videoIDs[] = $item->id->videoId;
}
}
$videosString = implode(",", $videoIDs);
$url = 'https://www.googleapis.com/youtube/v3/videos/?id=' . $videosString . '&part=statistics&key='.$API;
In return for this code, can you please tell me why you put .'' at the end of the $url assignment? I see this frequently and I never understand why people do it.
I am looking for a function that takes a dirty google search URL and returns it clean, as the original URL, means the URL that will show up in your browser after you clicked on the search result and the redirection.
For example, convert this link:
https://www.google.co.il/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiiz-Xp4srYAhXOxqQKHTZeAPQQFggnMAA&url=https%3A%2F%2Fwww.usatoday.com%2F&usg=AOvVaw04_mIwjwWapfFyzAJqqpNW
To this:
https://www.usatoday.com/
You can do this by 'exploding' the string.
<?php
$url = 'https://www.google.co.il/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiiz-Xp4srYAhXOxqQKHTZeAPQQFggnMAA&url=https%3A%2F%2Fwww.usatoday.com%2F&usg=AOvVaw04_mIwjwWapfFyzAJqqpNW';
$url = urldecode($url);
echo $url . '<br>'; //normal url
$url = explode('&',$url);
$url = str_replace('url=','',$url);
echo $url[9]; //the url is the 9th variable in the string/array
?>
I want to get the YouTube video ID from YouTube embed code using preg_match or regex. For a example
<iframe width="560" height="315" src="//www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>
I want to take the ID 0gugBiEkLwU
Can anyone tell me how to do this. Really appropriate your help.
Using this pattern with a capturing group should give you the string you want:
d\/(\w+)\?rel=\d+"
example: https://regex101.com/r/kH5kA7/1
You can use :
src="\/\/(?:https?:\/\/)?.*\/(.*?)\?rel=\d*"
Check Demo Here
Explanation :
I know this is pretty late, but I came up with something for people who might still be looking.
Since not all Youtube iframe src attributes end in "?rel=", and can sometimes end in another query string or end with a double quote, you can use:
/embed\/([\w+\-+]+)[\"\?]/
This captures anything after "/embed/" and before the ending double-quote/query string. The selection can include any letter, number, underscore and hyphen.
Here's a demo with multiple examples: https://regex101.com/r/eW7rC1/1
The below function will extract the youtube video id from teh all format of youtube urls,
function getYoutubeVideoId($iframeCode) {
// Extract video url from embed code
return preg_replace_callback('/<iframe\s+.*?\s+src=(".*?").*?<\/iframe>/', function ($matches) {
// Remove quotes
$youtubeUrl = $matches[1];
$youtubeUrl = trim($youtubeUrl, '"');
$youtubeUrl = trim($youtubeUrl, "'");
// Extract id
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $youtubeUrl, $videoId);
return $youtubeVideoId = isset($videoId[1]) ? $videoId[1] : "";
}, $iframeCode);
}
$iframeCode = '<iframe width="560" height="315" src="http://www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>';
// Returns youtube video id
echo getYoutubeVideoId($iframeCode);
How to get video.mp4 from vine url?
Example:
from https://vine.co/v/hnVVW2uQ1Z9
I need http://.../*.mp4 and http://.../*.jpg
Script what I need use this page vinebed.com
(In PHP)
Thanks much.
It's very simple. if you check the source of a vine video from vine.co you'll see the meta tags. and you should see twitter:player:stream. By using php you can extract that information specifically and use it like a variable.
<?php
function vine( $id )
{
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="twitter:player:stream" content="(.*?)"/', $vine, $matches);
$url = $_SERVER['REQUEST_URI'];
return ($matches[1]) ? $matches[1] : false;
}
?>
And to set an $id you will need to create a function that will either A) Automatically read a vine video id by url and you can display it like this <?php echo vine('bv5ZeQjY35'); ?> or B) Just set a vine video id and display as is.
Hope this helps as it's worked for me just fine.
I'm trying to pull out the video thumbnail from the TED video embed code. Why? Well, I'm using a WordPress theme that uses a custom field to handle video but the thumbnail function for that field isn't built for TED. I'm trying to re-jig it.
Here's the video thumbnail retrieval function (where YouTube and Vimeo are covered):
function woo_get_video_image($embed) {
$video_thumb = '';
/* Let's start by looking for YouTube, then Vimeo */
if ( preg_match( '/youtube/', $embed ) ) {
// YouTube - get the video code if this is an embed code (old embed)
preg_match( '/youtube\.com\/v\/([\w\-]+)/', $embed, $match);
// YouTube - if old embed returned an empty ID, try capuring the ID from the new iframe embed
if( !isset($match[1]) )
preg_match( '/youtube\.com\/embed\/([\w\-]+)/', $embed, $match);
// YouTube - if it is not an embed code, get the video code from the youtube URL
if( !isset($match[1]) )
preg_match( '/v\=(.+)&/',$embed ,$match);
// YouTube - get the corresponding thumbnail images
if( isset($match[1]) )
$video_thumb = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
} else if ( preg_match( '/vimeo/', $embed ) ) {
// Vimeo - get the video thumbnail
preg_match( '#http://player.vimeo.com/video/([0-9]+)#s', $embed, $match );
if ( isset($match[1]) ) {
$video_id = $match[1];
// Try to get a thumbnail from Vimeo
$get_vimeo_thumb = unserialize(file_get_contents_curl('http://vimeo.com/api/v2/video/'. $video_id .'.php'));
$video_thumb = $get_vimeo_thumb[0]['thumbnail_large'];
}
}
// return whichever thumbnail image you would like to retrieve
return $video_thumb;
}
Here's a typical TED embed:
<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
And the TED API docs if that helps at all: http://developer.ted.com/API_Docs
I seem to be having trouble customizing the preg_match and/or $get_vimeo_thumb portions (at least that's what I think is going on). Basically, I'm learning this portion of PHP and it's bumpy.
you can try this
$source = 'http://www.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes';
$tedJson = json_decode(file_get_contents('http://www.ted.com/talks/oembed.json?url='.urlencode($source)), TRUE);
pr($tedJson);
you will get the json in responce
I don't know what possessed me to answer this question, but here is a (tested working) quick and dirty. You'll probably want to throw some validation in there somewhere.. And if I were getting paid to do this it wouldn't be using file_get_contents and I'd probably use DOMDocument.
$embed = '<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>';
function getThumbnail($embed){
preg_match("/src\=\"(.+)?\"/", $embed, $matches);
$uri = $matches[1];
preg_match("/posterUrl\s=\s\'(.+)?\'/", file_get_contents($uri), $matches);
echo $matches[1];
}
getThumbnail($embed);
We are taking the src of the iframe, getting the contents, and scrapping the JS embed variable to grab the image they use for the thumbnail.
Obviously you won't echo the output, and who knows if this is against their TOS. As a matter of fact I'd bet they at least wouldn't let you use this unless you kept the logo (which is not the case). Use at your own risk.