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 =
url,json_encode(
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(
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.’"
}
}’;
access_token;
$result =
url,$data);
result);
return $final;
}