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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试