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

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( 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( 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, 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=". access_token;
$result = t h i s > h t t p s p o s t ( this->https_post( url,$data);
f i n a l = j s o n d e c o d e ( final = json_decode( result);
return $final;
}

发布了58 篇原创文章 · 获赞 0 · 访问量 2770

猜你喜欢

转载自blog.csdn.net/qq_30439399/article/details/102832337