<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; post-to-host</title>
	<atom:link href="https://zohead.com/archives/tag/post-to-host/feed/" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>PHP实现WordPress友言近期评论Widget</title>
		<link>https://zohead.com/archives/wordpress-uyan-recent-comment-widget/</link>
		<comments>https://zohead.com/archives/wordpress-uyan-recent-comment-widget/#comments</comments>
		<pubDate>Fri, 11 May 2012 16:16:35 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[post-to-host]]></category>
		<category><![CDATA[友言]]></category>
		<category><![CDATA[小工具]]></category>
		<category><![CDATA[社交网络]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[评论]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=120</guid>
		<description><![CDATA[本文同步自（如浏览不正常请点击跳转）：https://zohead.com/archives/wordpress-uyan-recent-comment-widget/ 现在有很多人都使用 WordPress 来搭建自己的博客系统，其中有一些是像我这样使用 友言 这个社交评论插件来替代系统原始的评论框的，友言 评论框插件对一些主流的社交网站支持比较好，很是推荐，友言官网： http://uyan.cc/ 之前我写过一个修改 友言 插件实现完美与 WordPress Mobile Pack 插件配合实现移动版博客的文章，在这里仅供参考： https://zohead.com/archives/m [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>本文同步自（如浏览不正常请点击跳转）：<a href="https://zohead.com/archives/wordpress-uyan-recent-comment-widget/" target="_blank">https://zohead.com/archives/wordpress-uyan-recent-comment-widget/</a></p>
<p>现在有很多人都使用 WordPress 来搭建自己的博客系统，其中有一些是像我这样使用 友言 这个社交评论插件来替代系统原始的评论框的，友言 评论框插件对一些主流的社交网站支持比较好，很是推荐，友言官网：</p>
<p><a href="http://uyan.cc/" target="_blank">http://uyan.cc/</a></p>
<p>之前我写过一个修改 友言 插件实现完美与 WordPress Mobile Pack 插件配合实现移动版博客的文章，在这里仅供参考：</p>
<p><a href="https://zohead.com/archives/modify-plugin-wordpress-mobile-pack/" target="_blank">https://zohead.com/archives/modify-plugin-wordpress-mobile-pack/</a></p>
<p>但现在发现还是有一点不足，由于使用了友言评论框替代 WordPress 本身的，导致 WordPress 的 ”近期评论“ Widget（小工具）显示不了，因此小折腾一个晚上，靠着还依稀记得的 PHP 编程经历，写了一个简单的显示 友言 近期评论的 WordPress Widget，先看效果图（本博客右侧就有 ^_^）：</p>
<p><a href="http://zohead.com/wp-content/uploads/wordpress-uyan-recent-comment-widget.png" target="_blank"><img src="http://zohead.com/wp-content/uploads/wordpress-uyan-recent-comment-widget.png" alt="友言近期评论Widget效果图" width="228" height="152" /></a></p>
<p><span style="color: #f00;"><strong>原理及代码：<br />
</strong></span></p>
<p>实现原理比较简单，先用工具分析 友言 评论的后台登录和评论显示之类的 HTTP 包信息，然后用 PHP 的 fsockopen 来自己发送 GET、POST 请求实现在 WordPress 上列举显示已经存在的友言评论（<span style="color: #006400;">备注：默认只显示已经经过审核的评论</span>）。</p>
<p>由于中间有几步 GET 和 POST 请求，就找了个现成的 <strong>post-to-host</strong> 这个很小的 PHP 脚本来发 GET 和 POST 请求，下载地址：</p>
<p><a href="http://code.google.com/p/post-to-host/" target="_blank">http://code.google.com/p/post-to-host/</a></p>
<p>下面是主程序 <span style="color: #006400;"><strong>uyan_comments.php</strong></span> 的代码：</p>
<pre class="brush: php; highlight: [3,4,5,6,8,13,36,40,72]; title: uyan_comments.php; notranslate">
&lt;?php
// global site information
$email = &quot;xxxx login email xxxx&quot;;
$password = &quot;xxxx encrypted password xxxx&quot;;
$domain = &quot;xxxx domain name xxxx&quot;;
$maxcomments = 10;

require_once(&quot;post_to_host.php&quot;);

header(&quot;Content-Type:text/html; charset=utf-8&quot;);

// Login to uyan.cc
$url = &quot;http://uyan.cc/index.php/youyan_login/userAutoLoginCrossDomain?email=&quot; . $email . &quot;&amp;loginPassword=&quot; . $password . &quot;&amp;rem=1&amp;domain=&quot; . $domain;

$ret_str = post_to_host($url, array(), array(), &quot;&quot;, $ret_head, 0);
$ret_str = trim($ret_str, &quot;()&quot;);

if ($ret_str == null || $ret_str == 'noData' || $ret_str == '&quot;noData&quot;') {
	echo(&quot;Failed to authentication with uyan.cc&quot;);
	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(&quot;Invalid data from uyan.cc&quot;);
	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 = &quot;http://uyan.cc/setting?uid=&quot; . $ret_json['uid'] . &quot;&amp;domain=&quot; . $domain . &quot;&amp;uname=&quot; . $ret_json['uname'];
$ret_str = post_to_host($url, array(), $arr_cookie, &quot;&quot;, $ret_head, 0);

// get comment
$url = &quot;http://uyan.cc/youyan_admin/getMoreCommentsByDomain/0&quot;;
$get_comment_params = array('currentMore' =&gt; '0', 'normalCommentToogle' =&gt; '1', 'readyCommentToogle' =&gt; '0', 'trashCommentToogle' =&gt; '0', 'delCommentToogle' =&gt; '0');

$ret_str = post_to_host($url, $get_comment_params, $arr_cookie, &quot;&quot;, $ret_head);

$comment_json = json_decode($ret_str, true);
$nr_comments = 0;

echo(&quot;&lt;ul&gt;&quot;);

foreach ($comment_json as $comment) {
	$display_name = &quot;&quot;;
	$display_title = &quot;&quot;;

	if ($maxcomments &gt;= 0 &amp;&amp; $nr_comments &gt;= $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']) &lt;= 0 || strlen($comment['page_title']) &lt;= 0)
		continue;

	// must with a valid display name
	if (array_key_exists('comment_author', $comment) &amp;&amp; strlen($comment['comment_author']) &gt; 0)
		$display_name = $comment['comment_author'];
	else if (array_key_exists('show_name', $comment) &amp;&amp; strlen($comment['show_name']) &gt; 0)
		$display_name = $comment['show_name'];
	else
		continue;

	$display_title = $comment['page_title'];
	$pos = strpos($display_title, ' | ');
	if ($pos &gt;= 0) $display_title = substr($display_title, 0, $pos);

	echo('&lt;li&gt;' . $display_name . '&amp;nbsp;&lt;strong&gt;&lt;font color=&quot;#0000FF&quot;&gt;&amp;gt;&amp;gt;&lt;/font&gt;&lt;/strong&gt;&amp;nbsp;&lt;a href=&quot;' . $comment['page_url'] . '&quot;&gt;' . $display_title . '&lt;/a&gt;&lt;/li&gt;');

	$nr_comments++;
}

echo(&quot;&lt;/ul&gt;&quot;);
?&gt;
</pre>
<p>代码本身比较简单，有些 PHP 基础就可以看懂了，把 <span style="color: #006400;"><strong>uyan_comments.php</strong></span> 文件最上面的 <span style="color: #006400;"><strong>$email</strong></span>、<span style="color: #006400;"><strong>$password</strong></span>、<span style="color: #006400;"><strong>$domain</strong></span>、<span style="color: #006400;"><strong>$maxcomments</strong></span> 改为实际的 友言 后台管理的登录邮箱、密码、你的域名、显示的最多评论数（备注：默认为10条，如果改为小于0的值则不限制显示的评论条数），就可以使用了。你应该已经发现这个 <span style="color: #006400;"><strong>uyan_comments.php</strong></span> 其实和 WordPress 没太大关系，完全也可以直接单独使用。</p>
<p>需要注意的是为了避免使用明文密码而可能导致的问题（安全第一 ^_^），上面的 <span style="color: #006400;"><strong>$password</strong></span> 是 友言 后台管理时实际用到的加密过的密码。这个加密过的密码可以通过 Firefox 的 Live HTTP headers 插件之类的抓取 HTTP 协议头的插件或工具来得到。</p>
<p><span style="color: #f00;"><strong>得到友言的加密登录密码：</strong></span></p>
<p>下面以 Firefox 的 Live HTTP headers 插件为例说明如何得到 友言 的实际加密的密码，打开 Live HTTP headers，该插件会自动开始抓取，然后用正确的邮箱和密码登录 友言 的后台管理，停止 Live HTTP headers 的抓取，在输出里就能找到地址为如下格式的 GET 请求，请求参数中就有加密的密码：</p>
<p><strong>http://uyan.cc/index.php/youyan_login/userAutoLoginCrossDomain?callback=jsonpxxxxxxx&amp;email=xxxxx@xxxxx.com&amp;loginPassword=<span style="color: #b22222;">xxxxxxxxxxxxxxxxx</span>&amp;rem=1&amp;domain=xxx.com<br />
</strong></p>
<p>其中的 <span style="color: #006400;"><strong>email</strong></span> 段就是登录邮箱，<span style="color: #006400;"><strong>loginPassword</strong></span> 段即为加密的密码，保存下该密码，修改 <span style="color: #006400;"><strong>uyan_comments.php</strong></span> 文件中的 <span style="color: #006400;"><strong>$email</strong></span> 和 <span style="color: #006400;"><strong>$password</strong></span> 值。</p>
<p>Live HTTP headers 的抓取截图如下所示（后面的未显示完整）：</p>
<p><a href="http://zohead.com/wp-content/uploads/uyan-login-http-headers.png" target="_blank"><img src="http://zohead.com/wp-content/uploads/uyan-login-http-headers.png" alt="友言后台管理的登录HTTP包" width="588" height="478" /></a></p>
<p><span style="color: #f00;"><strong>如何加入 WordPress Widget 列表中：</strong></span></p>
<p>你如果有真正实现一个 WordPress Widget 的心思，可以用本代码加上 WordPress 的 register_widget 之类的接口来实现。无奈我是一个超级懒人，懒人就有懒人的办法，下面介绍的就是懒人的办法，哈哈。</p>
<p>首先下载本文最下面下载链接中的 <span style="color: #006400;"><strong>post_to_host.php</strong></span> 和 <span style="color: #006400;"><strong>uyan_comments.php</strong></span>，将 <span style="color: #006400;"><strong>uyan_comments.php</strong></span> 中对应的 登录邮箱、密码、域名 改掉（参考上面），将这两个文件上传到 WordPress 根目录中（位置也可以自己修改），然后给 WordPress 安装 <strong>PHP Code Widget</strong> 插件，这是一个通用 Widget，添加之后，可以自行添加 文本、HTML、PHP 代码等，比较方便，插件地址：</p>
<p><a href="http://wordpress.org/extend/plugins/php-code-widget/" target="_blank">http://wordpress.org/extend/plugins/php-code-widget/</a></p>
<p>安装之后，在 WordPress 管理后台的 外观 - 小工具 里就能看到名为 “PHP Code”  的小工具，将其托至右侧的 “第一小工具区域”，输入自定义的标题，然后加入以下代码保存即可（如果上传的位置不在 WordPress 根目录那请自行修改）：</p>
<pre class="brush: php; highlight: [2]; title: 小工具的PHP代码; notranslate">
&lt;?php
include_once(&quot;uyan_comments.php&quot;);
?&gt;
</pre>
<p>重新访问 WordPress，如果上面的 登录邮箱、密码、域名 设定都正确的话，应该就可以出现类似上面效果图的评论列表。</p>
<p><span style="color: #f00;"><strong>不足和改进：</strong></span></p>
<p>1、uyan_comments.php 每次访问时都需要登录 友言 管理后台，请求评论列表，因此速度会有些影响，这个有空再改进了。</p>
<p>2、由于需要在 Web服务器中使用 PHP 的 fsockopen 来发 GET、POST 请求得到评论列表，因此可能对 WordPress 博客的访问速度造成一些影响，如果 PHP 空间在国内还可以，像我这样空间在国外的就稍微悲剧点了，所以建议安装 WP Super Cache WordPress 之类的插件来实现更好的缓存加速，将影响尽量降低。</p>
<p><span style="color: #f00;"><strong>下载地址（115网盘）：</strong></span></p>
<p><a href="http://115.com/file/beezjk3b" target="_blank">http://115.com/file/beezjk3b</a></p>
<p>写博客好累，准备休息，HOHO，本文件为个人作品，有任何问题欢迎指正。 ^_^</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/wordpress-uyan-recent-comment-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
