PHP实现WordPress友言近期评论Widget

本文同步自(如浏览不正常请点击跳转):https://zohead.com/archives/wordpress-uyan-recent-comment-widget/

现在有很多人都使用 WordPress 来搭建自己的博客系统,其中有一些是像我这样使用 友言 这个社交评论插件来替代系统原始的评论框的,友言 评论框插件对一些主流的社交网站支持比较好,很是推荐,友言官网:

http://uyan.cc/

之前我写过一个修改 友言 插件实现完美与 WordPress Mobile Pack 插件配合实现移动版博客的文章,在这里仅供参考:

https://zohead.com/archives/modify-plugin-wordpress-mobile-pack/

但现在发现还是有一点不足,由于使用了友言评论框替代 WordPress 本身的,导致 WordPress 的 ”近期评论“ Widget(小工具)显示不了,因此小折腾一个晚上,靠着还依稀记得的 PHP 编程经历,写了一个简单的显示 友言 近期评论的 WordPress Widget,先看效果图(本博客右侧就有 ^_^):

原理及代码:

实现原理比较简单,先用工具分析 友言 评论的后台登录和评论显示之类的 HTTP 包信息,然后用 PHP 的 fsockopen 来自己发送 GET、POST 请求实现在 WordPress 上列举显示已经存在的友言评论(备注:默认只显示已经经过审核的评论)。

由于中间有几步 GET 和 POST 请求,就找了个现成的 post-to-host 这个很小的 PHP 脚本来发 GET 和 POST 请求,下载地址:

http://code.google.com/p/post-to-host/

下面是主程序 uyan_comments.php 的代码:

<?php
// global site information
$email = "xxxx login email xxxx";
$password = "xxxx encrypted password xxxx";
$domain = "xxxx domain name xxxx";
$maxcomments = 10;

require_once("post_to_host.php");

header("Content-Type:text/html; charset=utf-8");

// Login to uyan.cc
$url = "http://uyan.cc/index.php/youyan_login/userAutoLoginCrossDomain?email=" . $email . "&loginPassword=" . $password . "&rem=1&domain=" . $domain;

$ret_str = post_to_host($url, array(), array(), "", $ret_head, 0);
$ret_str = trim($ret_str, "()");

if ($ret_str == null || $ret_str == 'noData' || $ret_str == '"noData"') {
	echo("Failed to authentication with uyan.cc");
	return;
}

$ret_json = json_decode($ret_str, true);

// check response JSON data
if (!array_key_exists('uid', $ret_json) || !array_key_exists('uname', $ret_json)) {
	echo("Invalid data from uyan.cc");
	return;
}

// delete unneeded 'auth' cookie key
$arr_cookie = get_cookies_from_heads($ret_head);
unset($arr_cookie['auth']);

// need this to set uid and uname
$url = "http://uyan.cc/setting?uid=" . $ret_json['uid'] . "&domain=" . $domain . "&uname=" . $ret_json['uname'];
$ret_str = post_to_host($url, array(), $arr_cookie, "", $ret_head, 0);

// get comment
$url = "http://uyan.cc/youyan_admin/getMoreCommentsByDomain/0";
$get_comment_params = array('currentMore' => '0', 'normalCommentToogle' => '1', 'readyCommentToogle' => '0', 'trashCommentToogle' => '0', 'delCommentToogle' => '0');

$ret_str = post_to_host($url, $get_comment_params, $arr_cookie, "", $ret_head);

$comment_json = json_decode($ret_str, true);
$nr_comments = 0;

echo("<ul>");

foreach ($comment_json as $comment) {
	$display_name = "";
	$display_title = "";

	if ($maxcomments >= 0 && $nr_comments >= $maxcomments) break;

	// must with valid URL and page title
	if (!array_key_exists('page_url', $comment) || !array_key_exists('page_title', $comment) || strlen($comment['page_url']) <= 0 || strlen($comment['page_title']) <= 0)
		continue;

	// must with a valid display name
	if (array_key_exists('comment_author', $comment) && strlen($comment['comment_author']) > 0)
		$display_name = $comment['comment_author'];
	else if (array_key_exists('show_name', $comment) && strlen($comment['show_name']) > 0)
		$display_name = $comment['show_name'];
	else
		continue;

	$display_title = $comment['page_title'];
	$pos = strpos($display_title, ' | ');
	if ($pos >= 0) $display_title = substr($display_title, 0, $pos);

	echo('<li>' . $display_name . '&nbsp;<strong><font color="#0000FF">&gt;&gt;</font></strong>&nbsp;<a href="' . $comment['page_url'] . '">' . $display_title . '</a></li>');

	$nr_comments++;
}

echo("</ul>");
?>

代码本身比较简单,有些 PHP 基础就可以看懂了,把 uyan_comments.php 文件最上面的 $email$password$domain$maxcomments 改为实际的 友言 后台管理的登录邮箱、密码、你的域名、显示的最多评论数(备注:默认为10条,如果改为小于0的值则不限制显示的评论条数),就可以使用了。你应该已经发现这个 uyan_comments.php 其实和 WordPress 没太大关系,完全也可以直接单独使用。

需要注意的是为了避免使用明文密码而可能导致的问题(安全第一 ^_^),上面的 $password 是 友言 后台管理时实际用到的加密过的密码。这个加密过的密码可以通过 Firefox 的 Live HTTP headers 插件之类的抓取 HTTP 协议头的插件或工具来得到。

得到友言的加密登录密码:

下面以 Firefox 的 Live HTTP headers 插件为例说明如何得到 友言 的实际加密的密码,打开 Live HTTP headers,该插件会自动开始抓取,然后用正确的邮箱和密码登录 友言 的后台管理,停止 Live HTTP headers 的抓取,在输出里就能找到地址为如下格式的 GET 请求,请求参数中就有加密的密码:

http://uyan.cc/index.php/youyan_login/userAutoLoginCrossDomain?callback=jsonpxxxxxxx&email=xxxxx@xxxxx.com&loginPassword=xxxxxxxxxxxxxxxxx&rem=1&domain=xxx.com

其中的 email 段就是登录邮箱,loginPassword 段即为加密的密码,保存下该密码,修改 uyan_comments.php 文件中的 $email$password 值。

Live HTTP headers 的抓取截图如下所示(后面的未显示完整):

如何加入 WordPress Widget 列表中:

你如果有真正实现一个 WordPress Widget 的心思,可以用本代码加上 WordPress 的 register_widget 之类的接口来实现。无奈我是一个超级懒人,懒人就有懒人的办法,下面介绍的就是懒人的办法,哈哈。

首先下载本文最下面下载链接中的 post_to_host.phpuyan_comments.php,将 uyan_comments.php 中对应的 登录邮箱、密码、域名 改掉(参考上面),将这两个文件上传到 WordPress 根目录中(位置也可以自己修改),然后给 WordPress 安装 PHP Code Widget 插件,这是一个通用 Widget,添加之后,可以自行添加 文本、HTML、PHP 代码等,比较方便,插件地址:

http://wordpress.org/extend/plugins/php-code-widget/

安装之后,在 WordPress 管理后台的 外观 - 小工具 里就能看到名为 “PHP Code”  的小工具,将其托至右侧的 “第一小工具区域”,输入自定义的标题,然后加入以下代码保存即可(如果上传的位置不在 WordPress 根目录那请自行修改):

<?php
include_once("uyan_comments.php");
?>

重新访问 WordPress,如果上面的 登录邮箱、密码、域名 设定都正确的话,应该就可以出现类似上面效果图的评论列表。

不足和改进:

1、uyan_comments.php 每次访问时都需要登录 友言 管理后台,请求评论列表,因此速度会有些影响,这个有空再改进了。

2、由于需要在 Web服务器中使用 PHP 的 fsockopen 来发 GET、POST 请求得到评论列表,因此可能对 WordPress 博客的访问速度造成一些影响,如果 PHP 空间在国内还可以,像我这样空间在国外的就稍微悲剧点了,所以建议安装 WP Super Cache WordPress 之类的插件来实现更好的缓存加速,将影响尽量降低。

下载地址(115网盘):

http://115.com/file/beezjk3b

写博客好累,准备休息,HOHO,本文件为个人作品,有任何问题欢迎指正。 ^_^