饭否/做啥应用之共同的好友

饭友布丁丁丁提过这样一则饭否应用的创意:找出自己和另外任一饭友之间共同的好友。今天我使用php将其粗略地实现了。步骤如下。

  1. 假设两位饭友分别为ffA和ffB。
  2. 通过饭否API得到ffA的好友列表,保存每个好友的ID至数组friendIDsA中,备用。
  3. 同理,得到ffB的好友列表ID数组friendIDsB中,备用。
  4. 求friendIDsA和friendIDsB的交集,保存至数组friendsList表中。
  5. 输出列表friendsList。

在实现时,具体细节:

  1. 使用GET的方法将需要处理的饭否ID传递给程序,而不是使用对话框、文本框的方式。
  2. 下载饭否好友列表,用到了curl函数。curl作为下载利器,很好用。
  3. 分析、获取好友信息,用到了xpath函数。xpath在xml中快速定位,很好用。另外可参阅《饭否消息解析之从minidom到xpath》一文。
  4. 获取A和B的交集时,使用了array_intersect函数。本函数是求两数组的交集。
  5. 在处理时,遇到“仅对好友公开类型”的饭友,将其略过。
  6. 输出列表时,界面仍然比较粗糙。本人对于网页的框架、CSS都知之甚少,也就不献丑了。

使用方法:先查一下两个人的饭否ID,然后修改下面的链接:
http://iregex.org/fanfou/friends.php?a=zhasm&b=passingby,更换其中的zhasm和passingby为您感兴趣的饭否ID。

另,”做啥”的也做出来了,格式如http://iregex.org/zuosa/friends.php?a=rexxer&b=alex

显示效果:如下图。


  1. Tears


  2. 令糊葱

  3. 如小果

  4. 妖娆男

  5. 流连

  6. 小北Roy

  7. 饭而不否

  8. 蛮子

  9. 如是如来

  10. 撕烤者

  11. 程小秸

  12. 王金牙儿

源代码:

<?php
function getFriendList($id)
{
    $url="http://api.fanfou.com/users/friends.xml?id=".$id; 
    //print $url;
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $output = curl_exec($ch); 
    curl_close($ch);
    $xml=simplexml_load_string($output);
    $result=$xml->xpath("//id");
    return $result;
}
 
function myGenerate($id)
{
    $url="http://api.fanfou.com/users/show.xml?id=".$id; 
    //print $url;
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $output = curl_exec($ch); 
    curl_close($ch);
    if ($output=="验证失败")
    {
        return;
    }
    $xml=simplexml_load_string($output);
    $name=$xml->xpath("//user/name");
    $img=$xml->xpath("//profile_image_url"); 
    $line="<li><a href=\"http://fanfou.com/$id\"><img src=\"$img[0][0]\" style=\"margin:0px 10 px 10px; border: 1px solid rgb(255, 255, 255); \"><br />$name[0]</a><br /><br /></li>";
    print $line; 
}
 
function myPrint($list)
{
    print "<ol>";
    foreach ($list as $value) 
    {
        myGenerate($value);
    }
    print "</ol>";
}
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
//to ensure the encoding system to be utf-8.
$a=$_GET["a"];
$b=$_GET["b"];
$my=getFriendList($a);
$his=getFriendList($b);
$our=array_intersect($my,$his);
myPrint($our);
?>
2008年11月22日09:00
  • StarKnight
    2008年11月22日01:16 | #1

    这个,有点像豆瓣的“××和我之间共同的喜好”……
    挺强大的……虽然我用不太到这功能:)

  • 2008年11月22日01:28 | #2

    @StarKnight 思路估计一样。觉得好玩才实现的,至于具体有什么实用价值,倒是没有考虑,呵呵。

  • 布丁丁丁
    2008年11月22日10:23 | #3

    rex,我去做个AUI玩玩看~~

  • 2008年11月22日10:27 | #4

    期待。

  • 2008年12月3日12:06 | #5

    嘿嘿,赞!
    貌似有个应用是说看要通过多少人才能找到2个人(是这样说的吧- -)..
    也差不多HO~

  • tears
    2009年3月22日14:46 | #6

    链接不好用了 请更新一个链接吧!

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">