Last active
January 3, 2023 17:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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