douzi9430 2011-05-13 17:30
浏览 42
已采纳

PHP YouTube API视频标题解析

I'm using some regexes to parse wiki-styled text.

<?php
function wikiParser($data){
 $data = preg_replace('/\[\[Youtube:([a-zA-Z0-9_]+)\]\]/', getYoutubeTitle("$1"), $data);
 return $data;
}
?>

This function searches for strings like [[Youtube:b32hRITAAew]] and calles another function getYoutubeTitle(b32hRITAAew).

<?php
function getYoutubeTitle($hash){
 $url = 'http://gdata.youtube.com/feeds/api/videos?v=2&q='.$hash.'&max-results=1&fields=entry(title)&prettyprint=true';
 $fp = fopen($url, 'r');
 $page = '';
 while(!feof($fp)){
  $page .= fgets($fp, 4096);
 }
 $titre = eregi("<title>(.*)</title>", $page, $regs);
 return $regs[1]; 
}
?>

The second function parses the response data. In the case of b32hRITAAew code, the following url is accessed

http://gdata.youtube.com/feeds/api/videos?v=2&q=b32hRITAAew&max-results=1&fields=entry(title)&prettyprint=true

It outputs:

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'>
    <entry>
        <title>The Lord of the Rings Symphony (1) HQ</title>
    </entry>
</feed>

And the title should be The Lord of the Rings Symphony (1) HQ. But for the unknown reason it shows me some random Photography Trick - Easy Image Stabilizer For Any Camera. I've worked hard to solve the issue, but still can't get it how that comes up. Is there any problem with getYoutubeTitle("$1") or anything else?

  • 写回答

1条回答 默认 最新

  • doudan5136 2011-05-13 17:48
    关注

    You can use the preg_replace_callback function like this:

    function wikiParser($data){
     $data = preg_replace_callback('/\[\[Youtube:([a-zA-Z0-9_]+)\]\]/', "getYoutubeTitle", $data);
     return $data;
    }
    
    function getYoutubeTitle($array){
     // $array looks like this: Array ( [0] => [[Youtube:b32hRITAAew]] [1] => b32hRITAAew )
     $hash = array_pop($array);
     $url = 'http://gdata.youtube.com/feeds/api/videos?v=2&q='.$hash.'&max-results=1&fields=entry(title)&prettyprint=true';
     ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题