本文主要和大家分享扫码关注和一键关注微信公众号的实现代码,希望能帮助大家更好的开发微信公众号功能。 * 获取一键关注授权标识
* */
public function getidentification()
{
$burl = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $this->access_tokens . "";
$result = curl_get($burl);
preg_match('/__biz.*&mid/', $result, $matches);//正则截取字符串
$svid = $this->get_between($matches[0], "__biz=", "==&mid");//截取出微信公众号唯一标识
$okurl="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=".$svid."==&scene=124#wechat_redirect";
jumpurl($okurl);
}
php自定义截取中间部分字符串方法,上面用到了,贴出来吧!
/* * php截取指定两个字符之间字符串 * */function get_between($input, $start, $end)
{ $substr = substr($input, strlen($start) + strpos($input, $start),
(strlen($input) - strpos($input, $end)) * (-1)); return $substr;}
微信公众号扫码关注代码
先上前后微信公众号扫码关注端不分离的代码<?php
header("content-type: text/html; charset=utf-8");
//http://pay.sucaihuo.com/project/access_token
//php获取微信access_token,appid和app_secret得到微信access_token
//php根据appid和secret获取微信access_token,php通过curl远程获取微信access_token信息
$appid = '自己公众号的appid';
$secret = '自己公众号的secret ';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";
$ch = curl_init();
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, 1);
$result = curl_exec($ch);
if($result == false)
{
echo 'curl error: ' . curl_error($ch);
}
curl_close($ch);
$access_tokens = json_decode($result, true);
//print_r($access_tokens);
$access_token = $access_tokens['access_token'];
function gettemporaryqrcode($access_token, $orderid) {
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token . "";
$qrcode = '{"expire_seconds": 1800, "action_name": "qr_scene", "action_info": {"scene": {"scene_id": ' . $orderid . '}}}';
$result = api_notice_increment($url, $qrcode);
$rs = json_decode($result, true);
return $rs;
// return urldecode($rs['url']);
}
$rs = gettemporaryqrcode($access_token, 1123);
//print_r($rs);
$ticket = $rs['ticket'];
$qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";
//print_r($qrcode);
function api_notice_increment($url, $data) {
$ch = curl_init();
// $header = "content-type: text/xml";
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_customrequest, "post");
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
// curl_setopt($ch, curlopt_httpheader, $header);
curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (compatible; msie 5.01; windows nt 5.0)');
curl_setopt($ch, curlopt_followlocation, 1);
curl_setopt($ch, curlopt_autoreferer, 1);
curl_setopt($ch, curlopt_postfields, $data);
curl_setopt($ch, curlopt_returntransfer, true);
$tmpinfo = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return $ch;
} else {
curl_close($ch);
return $tmpinfo;
}
}
?>
<p style="text-align: center;">
<p>关注素材火公众号</p>
<img src="<?php echo $qrcode; ?>" alt="关注公众号二维码" style="width:100px;height:100px;"/>
</p>
再放改成接口的代码post方式
class wxfollow
{
protected $appid = 'wxf1d959b99f33b156';
protected $secret = '248f3a560604555ec96215c085cb2723';
protected $url = "";
protected $access_tokens = "";
public function __construct()
{
//获取$access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret . "";
$result = curl_post($url);
$access_tokens = json_decode($result, true);
$this->access_tokens = $access_tokens['access_token'];
}
public function follow(){
//非必传项
$rs = $this->gettemporaryqrcode($this->access_tokens, 123);
$ticket = $rs['ticket'];
$qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";
///打印二维码显示
jumpurl($qrcode);
}
//生成二维码
public function gettemporaryqrcode($access_tokens,$orderid)
{
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" .$access_tokens . "";
//生成二维码需要的参数
$qrcode = '{"expire_seconds": 1800, "action_name": "qr_scene", "action_info": {"scene": {"scene_id": ' . $orderid . '}}}';
$momo = json_decode($qrcode, true);
$result = curl_post($url, $momo);
$rs = json_decode($result, true);
return $rs;
}
curl封装类
function curl_post($url, array $params = array())
{
$data_string = json_encode($params);
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_connecttimeout, 10);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_postfields, $data_string);
curl_setopt($ch, curlopt_httpheader,
array(
'content-type: application/json'
)
);
$data = curl_exec($ch);
curl_close($ch);
return ($data);
}
相关推荐:
通过php判断用户是否关注微信公众号
怎么根据微信id代码自动生成生成关注微信公众号二维码图片
php后台开发微信公众号实例
以上就是扫码关注和一键关注微信公众号的实现代码的详细内容。