掃碼關注和一鍵關注微信公眾號的實現代碼

來源:互聯網
上載者:User

本文主要和大家分享掃碼關注和一鍵關注公眾號的實現代碼,希望能協助大家更好的開發公眾號功能。

 * 擷取一鍵關注授權標識 * */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;}

公眾號掃碼關注代碼

先上前後公眾號掃碼關注端不分離的代碼

<?phpheader("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後台開發公眾號執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.