我的功能打破了网站

时间:2014-07-12 05:47:28

标签: php json youtube-data-api

<?php
if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { 
$id = $_SERVER['QUERY_STRING'];
$url = 'http://www.youtube.com/watch?v='.$id;

function get_youtube($url) {    
    $youtube = "http://www.youtube.com/oembed?url=" .$url. "&format=json";
    $json =  file_get_contents($youtube);
    return json_decode($json);
}

$json = get_youtube($url);

$thumb = $json->thumbnail_url;
$title = $json->title;
$artist = $json->author_name;
$thumb = $json->thumbnail_url;
$keywords = implode(", ", preg_split("/[\s]+/", $title));

echo '<title>MUZZIL | '.$title.'</title>';
echo '<meta name="description" content="Listen, watch &amp; download to '.$title.' by '.$artist.' on MUZZIL. Work easy, play easy. The perfect music tool for parties or casual listening.">';
echo '<meta name="keywords" content="'.$keywords.', '.$artist.', mp3, stream, official, lyrics, watch, free, music, song, playlist, youtube, download, listen, videos, MUZZIL, muzill">';
echo '<meta name="thumbnail" content="'.$thumb.'" />';

} else {
echo '<title>MUZZIL | Work Easy, Play Easy</title>';
echo '<meta name="description" content="Work easy, play easy. Listen, download and watch music free with auto playlist. The perfect music tool for parties or casual listening.">';
echo '<meta name="keywords" content="mp3, stream, official, lyrics, watch, free, music, song, playlist, youtube, download, listen, videos, MUZZIL, muzill">';
echo '<meta name="thumbnail" content="http://muzzil.com/img/lm.png" />';
}
?>

当我没有参数时,代码工作正常,但如果有URL参数则会中断。我已经设法找到get_youtube()的问题,但我找不到确切的原因。也没有生成错误日志。

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试我的网址= http://example.com/clone.php?5O86FUOrlkY检查empty()响应并返回

<?php
if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) { 
echo $id = $_SERVER['QUERY_STRING'];
$url = 'http://www.youtube.com/watch?v='.$id;

function get_youtube($url) {    
    $youtube = "http://www.youtube.com/oembed?url=" .$url. "&format=json";
    $json =  @file_get_contents($youtube);
    if(empty($json)) {
      return 'NO VIDEO FOUND';
     }  
    return json_decode($json, true);
}

$json = get_youtube($url);
print_r($json);
echo $thumb = $json['thumbnail_url']; //http://i1.ytimg.com/vi/5O86FUOrlkY/hqdefault.jpg
echo $title = $json['title']; //Finding Fanny | Official Trailer | Arjun Kapoor, Deepika 
echo $artist = $json['author_name']; //PadukoneFoxStarHindi
echo $thumb = $json['thumbnail_url'];
echo $keywords = implode(", ", preg_split("/[\s]+/", $title));

echo '<title>MUZZIL | '.$title.'</title>';
echo '<meta name="description" content="Listen, watch &amp; download to '.$title.' by '.$artist.' on MUZZIL. Work easy, play easy. The perfect music tool for parties or casual listening.">';
echo '<meta name="keywords" content="'.$keywords.', '.$artist.', mp3, stream, official, lyrics, watch, free, music, song, playlist, youtube, download, listen, videos, MUZZIL, muzill">';
echo '<meta name="thumbnail" content="'.$thumb.'" />';

} else {
echo '<title>MUZZIL | Work Easy, Play Easy</title>';
echo '<meta name="description" content="Work easy, play easy. Listen, download and watch music free with auto playlist. The perfect music tool for parties or casual listening.">';
echo '<meta name="keywords" content="mp3, stream, official, lyrics, watch, free, music, song, playlist, youtube, download, listen, videos, MUZZIL, muzill">';
echo '<meta name="thumbnail" content="http://muzzil.com/img/lm.png" />';
}
?>