我有这个代码,它在 Html 代码的div 上添加 YouTube 频道的视频(和视频信息)(_POST 美元):
var args= "url="+urlchaine;
xhr_object.open("POST", "traitement.php", true);
xhr_object.onreadystatechange = function() {
if(xhr_object.readyState == 4) {
eval(xhr_object.responseText);
}
return xhr_object.readyState;
}
xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr_object.send(args);
叛徒.php :
<?php
if ( isset($_POST["url"]) && !empty($_POST["url"]) )
$urlchaine = $_POST["url"];
else
$urlchaine = null;
$stringresult = str_replace("http://www.youtube.com/user/", "",$urlchaine);
require_once "Zend/Loader.php";
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
//Get Video info with channel name $stringresult
$videoFeed = $yt->getVideoFeed('http://gdata.youtube.com/feeds/users/...');
if ( $stringresult != null){
echo "var mydiv = document.getElementById('vids');";
echo "var newcontent = document.createElement('div');";
foreach ($videoFeed as $v): $thumbs = $v->getVideoThumbnails();
$videoId = $v->getVideoId();
$thumb = $thumbs[0]['url'];
$videoViewCount = $v->getVideoViewCount();
$videoTitle = $v->getVideoTitle();
echo "newcontent.innerHTML =
'<div class="videos">' +
' <div class="img_videos">' +
' <img class="img_video" width="250" idvideo="".$videoId."" ' +
' src="".$thumb.""/>' +
' </div>' +
' <h3>$videoTitle</h3>' +
' <p>$videoViewCount views</p>' +
'</div>' ;";
echo "mydiv.appendChild(newcontent.firstChild);";
endforeach;
?>
问题是,当我想这样做时,它与某些通道完美配合,而其他通道则存在错误(未捕获的语法错误:意外标识符)。经过几次测试,我发现通过删除$ videoTitle的显示,每个频道测试都有效。我的代码有什么问题?O.o
在某些情况下$videoTitle
似乎包含一个简单的引号,这会破坏您的JS代码。
尝试用这样的东西来转义这个引号:
str_replace("'", "‚", $videoTitle)