Comment wordpress vous permet-il d'intégrer des vidéos youtube/dailymotion/vimeo en utilisant simplement l'url? Par exemple, si vous tapez [youtube = http: //www.youtube.com/watch? V = cXXm696UbKY], la vidéo apparaîtra ici. Est-il possible de le faire en toute sécurité dans php en utilisant markdown?Autoriser l'intégration de vidéos dans les commentaires/textes
Répondre
La plupart (toutes?) De ces plates-formes vidéo offrent un support oEmbed. Par exemple, pour la vidéo YouTube http://www.youtube.com/watch?v=cXXm696UbKY
, il s'agit de http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%cXXm696UbKY
. Cela va retourner une réponse que vous pouvez facilement analyser avec json_decode
.
{
"provider_url": "http:\/\/www.youtube.com\/",
"title": "Auto-Tune the News #8: dragons. geese. Michael Vick. (ft. T-Pain)",
"html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>",
"author_name": "schmoyoho",
"height": 344,
"thumbnail_width": 480,
"width": 425,
"version": "1.0",
"author_url": "http:\/\/www.youtube.com\/user\/schmoyoho",
"provider_name": "YouTube",
"thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/bDOYN-6gdRE\/hqdefault.jpg",
"type": "video",
"thumbnail_height": 360
}
La partie intéressante est la propriété html
.
Tout ce que nous avons à faire est de rechercher le texte pour la balise [YouTube=...]
, d'extraire l'URL YouTube et de récupérer le code d'intégration via oEmbed.
Voici un exemple de travail:
<?php
function getYouTubeCode($url)
{
$oembedUrl = 'http://www.youtube.com/oembed?url=' . urlencode($url);
// The @-operator suppresses errors if the YouTube oEmbed service can't handle our request
$data = @file_get_contents($oembedUrl);
// If $data contains invalid JSON code, it will return null
$data = json_decode($data);
// So if $data is not an object now, we abort
if (!is_object($data)) {
return '';
}
// Otherwise we return the YouTube embed code
return $data->html;
}
$text = '<h1>Hi There</h1><p>You gotta watch this video:</p><p>[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]</p>';
$matches = array();
// We scan the $text variable for all occurrences of "[YouTube=<Any character except right squared bracket>]"
if (preg_match_all('/\[YouTube=([^\]]+)\]/i', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// Eg. $match[0] is "[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]"
// and $match[1] is "http://www.youtube.com/watch?v=cXXm696UbKY"
$text = str_replace($match[0], getYouTubeCode($match[1]), $text);
}
}
echo $text;
Je ne sais vraiment pas le problème avec wordpress, mais la logique de base est de rechercher l'URL et de le tourner vers le code intégré de Youtube, ajouter les choses autour de lui! Je pense que preg_replace()
est ce que vous devez garder à l'esprit!
Est-il tort de tomber en amour avec une réponse? – jonnnnnnnnnie
mais vous ne l'avez même pas +1. – erjiang
Mon mauvais, j'ai oublié :) fait – jonnnnnnnnnie