微信公众号订阅时回复两条信息

1,第一条是客户信息,第二条是订阅信息
2,先添加客服

function addkf()
{
    $access_token = $this->get_accessToken();
    $url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='.$access_token;

// $data = '{
// “kf_account” : "fangyangeg@",
// “nickname” : “客服1”,
// “password” : “systemsystem”,
// }’;
$data = array(
“kf_account”=>“fangyangeg@hunxiaofa”,
“nickname”=>“客服1”,
“password”=>“systemsystem”
);
$output = t h i s − > w x p o s t ( this->wx_post( this>wxpost(url,json_encode( d a t a , J S O N U N E S C A P E D U N I C O D E ) ) ; i f ( data,JSON_UNESCAPED_UNICODE)); if( data,JSONUNESCAPEDUNICODE));if(output == 0){
echo($output);
}
return false;
}
3,服务器地址URL http://www.
.cn/index.php/api/index/show
执行服务器地址URL

//连接微信公众号
public function show(){

    //获取随机字符串
    $echoStr = input("echostr");
    if($echoStr){
        // 验证接口的有效性,由于接口有效性的验证必定会传递echostr 参数
        if($this ->checkSignature()){
            echo $echoStr;
            exit;
        }
    }else{
        $this->responseMsg();
    }
}
 //消息推送

public function responseMsg()
{
    //get post data, May be due to the different environments
    $postStr = file_get_contents('php://input');
    //extract post data
    if (!empty($postStr)){

        /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                          the best way is to check the validity of xml by yourself */
        libxml_disable_entity_loader(true);
        $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( strtolower( $postObj->MsgType) == 'event'){
            //如果是关注 subscribe 事件
            if( strtolower($postObj->Event == 'subscribe') ){
                $msgType = "text";
                $content = "订阅信息。<a href='https://www.baidu.com'>测试</a>";
                $contentStr = "客服测试2链接:www.baidu.com/";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
                echo $resultStr;
                $this->messageToUserName($contentStr,$fromUsername);
            }
        }

    }else {
        echo "";
        exit;
    }
}

//客服消息
private function messageToUserName( c o n t e n t , content, content,fromUsername)//content 就是回复的消息,$fromUsername就是openid
{
//这里要获取token
$access_token = $this->get_accessToken();
KaTeX parse error: Expected '}', got 'EOF' at end of input: … "touser":"'.fromUsername.’",
“msgtype”:“text”,
“text”:
{
“content”:"’.$content.’"
}
}’;
u r l = " h t t p s : / / a p i . w e i x i n . q q . c o m / c g i − b i n / m e s s a g e / c u s t o m / s e n d ? a c c e s s t o k e n = " . url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=". url="https://api.weixin.qq.com/cgibin/message/custom/send?accesstoken=".access_token;
$result = t h i s − > h t t p s p o s t ( this->https_post( this>httpspost(url,$data);
f i n a l = j s o n d e c o d e ( final = json_decode( final=jsondecode(result);
return $final;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值