发布网友 发布时间:2022-04-21 22:40
共2个回答
热心网友 时间:2023-07-01 09:07
1.在微信共众平台开通你的账号(如果还没有的话),并在后台–>设置–>关键词自动回复里,启用公众平台消息接口
2.打开你的接口代码,开始编辑(下面是完整代码)
代码示例
微信公众平台整合小黄鸡PHP示例代码PHP
<?php
/**
* wechat php test
* 这基本就是官方的PHPSDK,改动很小
*/
//这里换成你的Token
define("TOKEN", "weixen");
$wechatObj = new wechatCallbackapiTest();
$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 e 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();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
if( $keyword == "help" ){//你说的这个,我懂,不劳小黄鸡烦心了,直接返回自定义结果
$contentStr = "帮助信息";
}
elseif( $keyword == "1" ){
$contentStr = "消息1";
}
elseif( $keyword == "2" ){
$contentStr = "消息2";
}
elseif( $keyword == "3" ){
$contentStr = "消息3";
}
else{
$contentStr = re( $keyword );//小黄鸡,你怎么看?(re调用小黄鸡)
}
//$contentStr = $contentStr."(自动回复,查看帮助请发help)";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}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;
}
}
}
//小黄鸡接口 感谢QQ:461106399提供的非官方接口
//参数 $keyword 为粉丝发来的消息,返回小黄鸡回复的消息
function re( $keyword ){
if ( $keyword<>'' ){
$header = array();
$header[]= 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * '. '/* ';
$header[]= 'Accept-Language: zh-cn ';
$header[]= 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1';
$header[]= 'Host: www.simsimi.com';
$header[]= 'Connection: Keep-Alive ';
$header[]= 'Cookie: JSESSIONID=2D96E7F39FBAB9B28314607D0328D35F';
$Ref="http://www.simsimi.com/talk.htm?lc=ch";
$Ch = curl_init();
$Options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_URL => 'http://www.simsimi.com/func/req?msg='.$keyword.'&lc=ch',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_REFERER => $Ref,
);
curl_setopt_array($Ch, $Options);
$Message = json_decode(curl_exec($Ch),true);
curl_close($Ch);
if($Message['result']=='100' && $Message['response'] <> 'hi'){
return $Message['response'];
}else{
echo '微信论坛服务器异常,请联系管理员';
}
}
}
?>
热心网友 时间:2023-07-01 09:07
启用开发模式,实体书教程里面有小黄鸡代码,网上估计也能下载到