一:前往微信公众平台基本配置,配置服务器配置 二:服务器地址URL要对应控制器中的方法
public function valid(){
$xml_str = file_get_contents("php://input");
$xml = (array)simplexml_load_string($xml_str,"SimpleXMLElement",LIBXML_NOCDATA);
if ($xml['MsgType'] == 'event')
{
if ($xml['Event'] == 'subscribe')
{
if(empty($xml['EventKey'])){
$res = new Wechattwo();
$a = $res -> getUserMessage($xml['FromUserName'],0);
if($a){
$msg = '感谢关注';
}
}else{
$uid = str_replace('qrscene_','',$xml['EventKey']);
$res = new Wechattwo();
$a = $res -> getUserMessage($xml['FromUserName'],$uid);
if($a){
$msg = '已经关注过了';
}
}
echo $this->sendText($msg,$xml);
die;
}
if ($xml['Event'] == 'unsubscribe')
{
echo "取消关注";die;
}
}
}
三:扫描二维码(我生成的是静态二维码)直接通过app_id 和 app_secret 获取(没有获取code)
public function getToken($appid,$appsecret){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->http_curl($url, 'get');
$access_token = $token['access_token'];
return $access_token;
}
public function qrCode($accesstoken,$uid){
$u = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $accesstoken;
$param = array();
$param['action_name'] = "QR_LIMIT_SCENE";
$param['action_info'] = array('scene' => array('scene_id' => $uid));
$param = json_encode($param);
$res = $this->http_curl($u, 'post', 'json', $param);
$qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $res['ticket'];
return $qrcode;
}
附加:这个是curl接口代码
public function http_curl($url, $type = 'get', $res = 'json', $arr = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($type == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
$output = curl_exec($ch);
if ($res == 'json') {
if (curl_errno($ch)) {
return curl_error($ch);
} else {
return json_decode($output, true);
}
}
curl_close($ch);
}
这样扫描二维码就直接进入公众号关注页面了。
|