求帮忙看下微信公众平台接口的代码

2013-03-25 22:34:47 +08:00
 laoyu
因为微信公众平台26号更新,所以怕以下代码失效。

“新用户订阅,将由之前推送一条“Hello2BizUser”文本,变化为推送一条“subscribe”的事件。”

不懂这个,哪问朋友指点一下该怎么改,应该会受影响吧。



===================代码如下=====================




<?php
/*
Plugin Name: 微信公众平台搜索插件
Plugin URI: http://ftqq.com/
Description: 微信公众平台插件,允许用户通过微信关键字获取相关的文章内容。假设当前wordpress的地址为http://ftqq.com,那么安装完插件后,在微信中需要填写的接口地址为http://ftqq.com/wp-content/plugins/wx-search/wx-search.php
Version: 1.0
Author: easychen@qq.com
Author URI: http://ftqq.com
License: BSD
*/


// 此token必须和微信公众平台中的设置保持一致
// 设置页面 http://mp.weixin.qq.com/cgi-bin/callbackprofile?t=wxm-callbackapi&type=info&lang=zh_CN
define("TOKEN", "xxxxx");


// 此图片用于搜索出来的文章不包含图片时的默认图片,直接从wordpress的媒体库中挑一张即可。
// 采用相对路径
define("DEFAULT_COVER", "/#");

define("WELCOME" , "欢迎您......");

// 假设当前wordpress的地址为http://ftqq.com
// 那么安装完插件后,在微信中需要填写的接口地址为http://ftqq.com/wp-content/plugins/wx-search/wx-search.php


// 以下内容不需要改动

$wechatObj = new wechatCallbackapiTest();


if( isset($_REQUEST['echostr']) )
$wechatObj->valid();
elseif( isset( $_REQUEST['signature'] ) )
{
chdir('../../../');
include( 'wp-load.php' );
$wechatObj->responseMsg();
}



class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];

//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//extract post data
if (!empty($postStr)){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();

if(!empty( $keyword ))
{
//file_put_contents( 'keyword.txt' , $keyword );

if($articles = ws_get_article( $keyword ))
{
ob_start();
?><xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=$time?></CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[搜索结果]]></Content>
<ArticleCount><?=count($articles)?></ArticleCount>
<Articles><?php foreach( $articles as $item ): ?>
<item>
<Title><![CDATA[<?=$item['title']?>]]></Title>
<Description><![CDATA[<?=$item['content']?>]]></Description>
<PicUrl><![CDATA[<?=$item['pic']?>]]></PicUrl>
<Url><![CDATA[<?=$item['url']?>]]></Url>
</item>
<?php endforeach; ?></Articles>
<FuncFlag>0</FuncFlag>
</xml><?php
$xml = ob_get_contents();
//file_put_contents('xml.txt', $xml);
header('Content-Type: text/xml');
echo trim($xml);

}else
{
if( $keyword == 'Hello2BizUser' )
{?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<?=WELCOME?>]]></Content>
</xml>
<?php }
else{
?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[没有找到包含关键字的文章,试试其他关键字?]]></Content>
</xml>
<?php
} }
}else{
echo "请输入关键字,我们将返回对应的文章...";
}

}else {
echo "";
exit;
}
}

private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}

function ws_get_article( $keyword , $limit = 10 )
{
query_posts( array( 's' => $keyword ) );
$i = 0;
$results = array();
while( have_posts() && ($i < 10) )
{
the_post();
$result['title'] = get_the_title();
$result['content'] = mb_strimwidth(get_the_content() , 0 , 200 , '...' , 'UTF-8' );
$result['url'] = get_site_url().'/?p='.get_the_id();
$result['pic'] = thumbnail_url(get_the_post_thumbnail());
if( !$result['pic'] ) $result['pic'] = get_site_url(). DEFAULT_COVER;



$results[] = $result;
$i++;
}

if( count( $results ) > 0 ) return $results ;
else return false;

}

function thumbnail_url( $html )
{
$reg = '/src="(.+?)"/is';
if(preg_match( $reg , $html , $out ))
{
return $out[1];
}

return false;
}
4331 次点击
所在节点    程序员
25 条回复
tempdban
2013-03-26 23:03:31 +08:00
@wzxjohn 恩 我有个问题 你们平时是怎么测试的呢 直接用生产环境
laoyu
2013-03-26 23:08:16 +08:00
@tempdban 开小号,我只会用这种最笨的办法。
wzxjohn
2013-03-27 08:37:55 +08:00
@tempdban
开小号+1。不然没办法。我手里有一堆微信号= =|||
wzxjohn
2013-03-27 08:42:16 +08:00
@Lucius

$keyword = trim($postObj->Event);
改成
if ($keyword == "subscribe")
{?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<?=WELCOME?>]]></Content>
</xml>
<?php }

同时将
if( $keyword == 'Hello2BizUser' )
这段删去就可以了。把event类型的消息提前处理掉,还能省去搜索文章的时间。
@laoyu
itommy
2013-03-27 12:16:46 +08:00
@wzxjohn
@laoyu
@tempdban
为什么不在local上测试这个?写个class就可以在browser里面测试啦。而且如果你把token和$postObj->ToUserName 也加进去一个接口可以对应无数个微信号。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/64013

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX