Php 简单的Youtube API查询并保存到变量

Php 简单的Youtube API查询并保存到变量,php,search,youtube,Php,Search,Youtube,我试图通过api搜索youtube,然后将搜索结果保存到一个变量中,然后回显。很难让它工作!我已经在html中包含了整个代码。我不确定是否与加载youtube脚本库有关,或者语法错误的多个。谢谢 <html> <body> <?php $params="puppy"; function youtube_find_video($params) { str_replace("'", "", $params); $q = preg_replace('/[[

我试图通过api搜索youtube,然后将搜索结果保存到一个变量中,然后回显。很难让它工作!我已经在html中包含了整个代码。我不确定是否与加载youtube脚本库有关,或者语法错误的多个。谢谢

<html>
<body>
<?php
$params="puppy";
function youtube_find_video($params)
{
    str_replace("'", "", $params);
    $q = preg_replace('/[[:space:]]/', '/', trim($params));
    $q = utf8_decode(utf8_encode($q));
    $replacements = array(',', '?', '!', '.');
    $q = str_replace($replacements, "", $q);
    $feedURL = "http://gdata.youtube.com/feeds/api/videos/-/{$q}?orderby=relevance&max-results=1";
    $sxml = simplexml_load_file($feedURL);
    if(!$sxml)
    {
        return false;
    }       
    else{
        $entry  = $sxml->entry;
        if(!$entry)
        {
            return false;
        }

        // get nodes in media: namespace for media information
        $media = $entry->children('http://search.yahoo.com/mrss/');

        if($media)
        {
            // get video player URL
            $attrs = $media->group->player->attributes();
            $url = $attrs['url'];
            if(!$url)
            {
                return false;
                break;
            }
            parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
            $watch['id'] = $my_array_of_vars['v'];

            // get video name
            $watch['name'] = $media->group->title;

            // get <yt:duration> node for video length[minute]
            $yt = $media->children('http://gdata.youtube.com/schemas/2007');
            $attrs = $yt->duration->attributes();
            $watch['length'] = sprintf("%0.2f", $attrs['seconds']/60);

            $watch = simplexml_kurtul($watch); 
            return $watch;           
echo $watch;
}

        else
        {
            return false;
        }
    }


} 
youtube_find_video();
?>
  </body>
</html>

您正在调用
youtube\u find\u video()
,但未使用
$params
。将PHP的最后一行更改为:

youtube_find_video($params);

也请给出你得到的错误。如果不知道出了什么问题,就无法提供帮助。

您会遇到哪些确切的错误?奇怪的行为是什么?我建议你使用Zend Gdata库,使类似的东西成为一行程序。你能举一个一行程序的例子吗?对不起,我有点傻。有没有一种简单的方法可以查询youtube并将数据保存在变量中?