饭友布丁丁丁提过这样一则饭否应用的创意:找出自己和另外任一饭友之间共同的好友。今天我使用php将其粗略地实现了。步骤如下。
- 假设两位饭友分别为ffA和ffB。
- 通过饭否API得到ffA的好友列表,保存每个好友的ID至数组friendIDsA中,备用。
- 同理,得到ffB的好友列表ID数组friendIDsB中,备用。
- 求friendIDsA和friendIDsB的交集,保存至数组friendsList表中。
- 输出列表friendsList。
在实现时,具体细节:
- 使用GET的方法将需要处理的饭否ID传递给程序,而不是使用对话框、文本框的方式。
- 下载饭否好友列表,用到了curl函数。curl作为下载利器,很好用。
- 分析、获取好友信息,用到了xpath函数。xpath在xml中快速定位,很好用。另外可参阅《饭否消息解析之从minidom到xpath》一文。
- 获取A和B的交集时,使用了array_intersect函数。本函数是求两数组的交集。
- 在处理时,遇到“仅对好友公开类型”的饭友,将其略过。
- 输出列表时,界面仍然比较粗糙。本人对于网页的框架、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
显示效果:如下图。
源代码:
<?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); ?> |

真是多事之秋。也罢,莫谈国是。说一说饭否得了。
但是,就没有解决办法了吗?非也非也。解决之道就是老夫一直提倡饭友使用、一直希望饭否官方提供支持的万能无敌TAG术: