1.推荐者生成带参数二维码,这里我已经写过,就不再写了;写下获取access_token
function __construct()
{
$this->appid = config('APPID');
$this->secrect = config('APPSECRET');
$this->accessToken = $this->getToken($this->appid, $this->secrect);
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* 获取token
*/
protected function getToken($appid, $appsecret)
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
return $access_token;
}
2.用户扫码事件处理
//获取用户关注事件
public function urlRedirect(){//公众号配置路径必须是这里,接收关注后微信传过来的参数
if(!isset($_GET['echostr'])){
$this->responseMsg();
}else{
$this->valid();
}
}
public function valid(){
$nonce = $_GET['nonce'];
$token = '******************************';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后与signature进行校验
$str = sha1( implode( $array ) );
if( $str == $signature ){
//第一次接入weixin api接口的时候
echo $echostr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE){
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
0
";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "欢迎您关注好运平台!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "请输入...";
}
}
public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "感谢您关注【*******】";
// $contentStr = "感谢您关注【********】"."\n".$object->ToUserName."\n".$object->FromUserName."\n".$object->FromUserName."\n".$object->MsgType."\n".$object->Event."\n".$object->EventKey."\n".$object->Ticket;
$openid = (string)$object->FromUserName; //数据类型转换为字符串,mmp这个问题找了好久
$refer_id = explode('_',$object->EventKey); //$object->EventKey返回的是qrsence_123这种类型
$this->createuserinfo($openid,$refer_id[1]);//获取用户信息
break;
case "SCAN":
$contentStr = "您已关注过,谢谢!";
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}
public function responseText($object, $content, $flag=0)
{
$textTpl = "
%s
%d
";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
3.获取用户信息,绑定关系并入库
//用户关注后获取用户信息
public function createuserinfo($openid,$refer_id)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $this->accessToken . "&openid=" . $openid;
$user= $this->request_get($url);
$user = json_decode($user,true);
$users = array(
'openid' =>$openid,
'nickname' =>$user['nickname'],
'avatar' =>$user['headimgurl'],
'sex' =>$user['sex'],
'unionid' =>$user['unionid'],
'status' => 1,
'reg_time' =>$user['subscribe_time'],//关注公众号的时间
'bind_user'=>$refer_id
);
db('user')->insert($users);
}
4.部分基础函数的调用
/**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 发送get请求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}