两种方式:
第一种类似于公众号的消息接受与发送,适合小程序
首先登陆小程序后台
在开发设置中,开启消息推送服务,填写你的服务器url地址,token,选择json数据格式
php的微信小程序客服接受消息和推送消息代码
<?php define("TOKEN", "213123123123!1232"); //填写自己的token,跟小程序后台填的一样即可 if (isset($_GET['echostr'])) { //校验服务器地址URL valid(); }else{ responseMsg(); } function valid() { $echoStr = $_GET["echostr"]; if(checkSignature()){ header('content-type:text'); echo $echoStr; exit; }else{ echo $echoStr.'+++'.TOKEN; exit; } } function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } function responseMsg() { $postStr = file_get_contents('php://input'); //此处推荐使用file_get_contents('php://input')获取后台post过来的数据 if (!empty($postStr) && is_string($postStr)){ $postArr = json_decode($postStr,true); if(!empty($postArr['MsgType']) && $postArr['Content'] == "1"){ //用户发送1,回复公众号二维码 $fromUsername = $postArr['FromUserName']; //发送者openid //$imgurl = "/300-300.png"; //公众号二维码,相对路径,修改为自己的 // $media_id = getMediaId($imgurl); //获取图片消息的media_id // $data=array( // "touser"=>$fromUsername, // "msgtype"=>"image", // "image"=>array("media_id"=>$media_id) // ); $content = '你好,回复1关注公众号,回复2获取官网链接'; $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+ requestAPI($json); } elseif(!empty($postArr['MsgType']) && $postArr['Content'] == "2"){ //用户发送2,回复图文链接 $fromUsername = $postArr['FromUserName']; //发送者openid $data=array( "touser"=>$fromUsername, "msgtype"=>"link", "link"=>array( //修改下面几项为自己的 "title"=>'bfw爱编程', "description"=>'bfw爱编程,专业的技术社区', "url"=>'https://www.bfw.wiki/', "thumb_url"=>'https://repo.bfw.wiki/bfwrepo/icon/60ecf359eb0c5.png', ) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+ requestAPI($json); } elseif(!empty($postArr['MsgType']) && $postArr['Content'] == "3"){ //用户发送3,回复文字 $fromUsername = $postArr['FromUserName']; //发送者openid $content = '你好,回复1关注公众号,回复2获取官网链接'; //修改为自己需要的文字 $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+ requestAPI($json); } elseif(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){ //用户发送图片,这里示例为回复他公众号二维码 $fromUsername = $postArr['FromUserName']; //发送者openid $imgurl = "/300-300.png"; //公众号二维码,相对路径,修改为自己的 $media_id = getMediaId($imgurl); //获取图片消息的media_id $data=array( "touser"=>$fromUsername, "msgtype"=>"image", "image"=>array("media_id"=>$media_id) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4以上版本才可使用 requestAPI($json); } elseif($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ //用户进入客服后马上回复,现在已失效,需要用户先发过消息 $fromUsername = $postArr['FromUserName']; $content = '你好,回复1关注公众号,回复2获取官网链接'; $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+ requestAPI($json); } elseif($postArr['MsgType'] !== 'event'){ //用户发送其他内容,引导加客服 $fromUsername = $postArr['FromUserName']; //发送者openid $imgurl = "/miniapp300-300.png"; //客服微信二维码,相对路径 $media_id = getMediaId($imgurl); //获取图片消息的media_id $data=array( "touser"=>$fromUsername, "msgtype"=>"image", "image"=>array("media_id"=>$media_id) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+ requestAPI($json); } else{ exit; } }else{ echo "empty"; exit; } } function requestAPI($json){ $access_token = get_accessToken(); /* * POST发送https请求客服接口api */ $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token; //以'json'格式发送post的https请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($json)){ curl_setopt($curl, CURLOPT_POSTFIELDS,$json); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers ); $output = curl_exec($curl); if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); if($output == 0){ echo 'success';exit; } } /* 调用微信api,获取access_token,有效期7200s*/ function get_accessToken(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1231231321317&secret=12312313123122f376c6bf0e3a8'; //替换成自己的小程序id和secret @$weixin = file_get_contents($url); @$jsondecode = json_decode($weixin); @$array = get_object_vars($jsondecode); $token = $array['access_token']; return $token; } //获取上传图片的media_id function getMediaId($imgurl){ $token=get_accessToken(); $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image"; // echo $url; $ch1 = curl_init(); $timeout = 10; $real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";//自动转成图片文件绝对路径,如果图片发送失败,检查PHP的$_SERVER['DOCUMENT_ROOT'的配置 // echo $real_path; $data = array("media" => new CURLFile("{$real_path}"));//php5.6(含)以上版本使用此方法 // var_dump($data); curl_setopt($ch1, CURLOPT_URL, $url); curl_setopt($ch1, CURLOPT_POST, 1); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch1, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch1); // echo $result; curl_close($ch1); if($result){ $result = json_decode($result, true); return $result['media_id']; } else{ return null; } } ?>
第二种是企业微信的微信客服,可以接入小程序、app、网页等地方。
网友回复