dtamho6404 2014-01-13 16:00
浏览 24
已采纳

Php Javascript Youtube API错误

I've got this code, which add videos (and videos infos) of a YouTube channel ($_POST) on a div of the Html code :

    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);

traitement.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;
?>

The problem is, when I want to do that, it works perfectly with some channels, whereas an error has existed for others (Uncaught SyntaxError: Unexpected identifier). After several tests, I saw that by removing the display of $ videoTitle, every channels test worked. What is wrong with my code? O.o

  • 写回答

1条回答 默认 最新

  • duancong6937 2014-01-13 16:03
    关注

    $videoTitle seems to contains a simple quote in somes cases, which break your JS code.

    Try escaping this quotes with something like this :

    str_replace("'", "&sbquo;", $videoTitle)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog