Skip to content

Instantly share code, notes, and snippets.

@davebarnwell
Last active January 3, 2023 17:10
Show Gist options
  • Select an option

  • Save davebarnwell/c7a08c5cc52675f2b819 to your computer and use it in GitHub Desktop.

Select an option

Save davebarnwell/c7a08c5cc52675f2b819 to your computer and use it in GitHub Desktop.
php generate embed (video embed) info for a given youtube or vimeo url
<?php
/**
* Class Oembed
*/
class Oembed
{
/**
* Given a vimeo or youtube URL get the oembed info
*
* @param string $video_url
*
* @return null|\stdClass property html has the emebed iframe code or similar
*/
public static
function getInfo( $video_url ) {
$oembed = 'https://www.youtube.com/oembed';
if (preg_match( '|^http(s)?://(.*?)vimeo.com|', $video_url )) {
$oembed = 'https://vimeo.com/api/oembed.json';
}
$oembed .= '?format=json&url=' . urlencode( $video_url );
$json_string = @file_get_contents( $oembed );
/**
* @var null|\stdClass
*/
$obj = json_decode( $json_string );
if ($obj == null || !$obj) {
return null;
}
return $obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment