最近接入了小程序客服,源码放在这里以备后用
public function check_server(){//校验服务器地址URL
if (isset($_GET['echostr'])) {
$this->valid();
}else{
$this->responseMsg();
}
}
public function valid()
{
$echoStr =$_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}else{
echo $echoStr.'+++'.'pksfc';
exit;
}
}
public function responseMsg()
{
$postStr =$GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr) && is_string($postStr)){
//禁止引用外部xml实体
//libxml_disable_entity_loader(true);
//$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$postArr =json_decode($postStr,true);
if(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='text'){//文本消息
$fromUsername =$postArr['FromUserName']; //发送者openid
$toUserName =$postArr['ToUserName']; //小程序id
$textTpl =array(
"ToUserName"=>$fromUsername,
"FromUserName"=>$toUserName,
"CreateTime"=>time(),
"MsgType"=>"transfer_customer_service",
);
exit(json_encode($textTpl));
}elseif(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='image'){//图文消息
$fromUsername =$postArr['FromUserName']; //发送者openid
$toUserName =$postArr['ToUserName']; //小程序id
$textTpl =array(
"ToUserName"=>$fromUsername,
"FromUserName"=>$toUserName,
"CreateTime"=>time(),
"MsgType"=>"transfer_customer_service",
);
exit(json_encode($textTpl));
}elseif($postArr['MsgType'] =='event' &&$postArr['Event']=='user_enter_tempsession'){//进入客服动作
$fromUsername =$postArr['FromUserName']; //发送者openid
$content ='您好,有什么能帮助你?';
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"text",
"text"=>array("content"=>$content)
);
$json =$this->json_encode($data); //php5.4+
$access_token =$this->getAccessToken();
/*
* 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;
}
}else{
exit('aaa');
}
}else{
echo "";
exit;
}
}
private function checkSignature()//官方的验证函数
{
$signature =$_GET["signature"];
$timestamp =$_GET["timestamp"];
$nonce =$_GET["nonce"];
$token ='pksfc';
$tmpArr =array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr );
$tmpStr = sha1($tmpStr );
if($tmpStr ==$signature ){
return true;
}else{
return false;
}
}
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data =json_decode($this->get_php_file("access_token.php"));
if ($data->expire_time < time()) {
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxeb81a27d58c415af&secret&secret=53a886e372afeadc0f9e86f3b1a4549c";
$res =json_decode($this->curl_https($url));
$access_token =$res->access_token;
if ($access_token) {
$data->expire_time = time() +7000;
$data->access_token =$access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
}else {
$access_token =$data->access_token;
}
return $access_token;
}
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "" .$content);
fclose($fp);
}
function curl_https($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
function json_encode($array)
{
if(version_compare(PHP_VERSION,'5.4.0','<')){
$str =json_encode($array);
$str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
},$str);
return $str;
}else{
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
}