微信二維碼 場景二維碼 用於推送事件,關注等 註冊用戶 ,通過測試

/** 
 * Created by PhpStorm. 
 * User: Administrator 
 * Date: 2015-10-01 
 * Time: 15:41 
 */  
/** 
 * * Created by PhpStorm. 
 * * User: bin 
 * * Date: 15-1-16 
 * * Time: 上午9:48 
 * */  
  
//根據網上的代碼 改寫 不須要<span style="color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; line-height: 22px;"><span style="color: rgb(128, 0, 128);">openssl </span></span>支持和 libconv 支持  
//namespace HomeCommon;  
// 微信處理類  
  
set_time_limit(30);  
  
  
class Weixin  
{  
//構造方法  
  
    static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";  
  
    static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";  
  
    static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";  
  
   //生成二維碼  
    //$wechat = array('appid'=>'','secret'=>'' );  
    public function getEwm($wechat, $fqid, $type = 1)  
    {  
  
        //$wechat = array('appid'=>'','secret'=>'' );  
  
        $appid = $wechat['appid'];  
  
        $secret = $wechat['secret'];  
  
        $ACCESS_TOKEN = $this->getToken($appid, $secret);  
  
        $url = $this->getQrcodeurl($ACCESS_TOKEN, $fqid, 1);  
  
        return $this->DownLoadQr($url, time());  
  
    }  
  
    protected function getQrcodeurl($ACCESS_TOKEN, $fqid, $type = 1)  
    {  
  
        $url = self::$qrcode_url . 'access_token=' . $ACCESS_TOKEN;  
        //echo $url;  
  
        if ($type == 1) {  
            //生成永久二維碼  
  
            $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": ' . $fqid . '}}}';  
  
        } else {  
            //生成臨時二維碼  
  
            $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ' . $fqid . '}}}';  
  
        }  
  
        $result = $this->http_post_data($url, $qrcode);  
  
        //print_r($result);  
        $oo = json_decode($result[1]);  
  
        if (!$oo->ticket) {  
  
            $this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');  
  
            exit();  
  
        }  
  
        $url = self::$qrcode_get_url . 'ticket=' . $oo->ticket . '';  
  
        return $url;  
  
    }  
  
    protected function getToken($appid, $secret)  
    {  
        //echo self::$token_url . "appid=$appid&secret=$secret";  
        $ACCESS_TOKEN = $this->curl_get_contents(self::$token_url . "appid=$appid&secret=$secret");  
  
        $ACCESS_TOKEN = json_decode($ACCESS_TOKEN);  
        if($ACCESS_TOKEN){  
            //print_r($ACCESS_TOKEN);  
            $ACCESS_TOKEN = $ACCESS_TOKEN->access_token;  
        }else{  
  
            $ACCESS_TOKEN = '';  
        }  
        return $ACCESS_TOKEN;  
  
    }  
  
    protected function http_post_data($url, $data_string)  
    {  
  
        //echo $url;  
        //echo $data_string;  
  
        $ch = curl_init();  
  
        curl_setopt($ch, CURLOPT_POST, 1);  
  
        curl_setopt($ch, CURLOPT_URL, $url);  
  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
  
        @curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
  
  
  
  
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
                'Content-Type: application/json; charset=utf-8',  
                'Content-Length: ' . strlen($data_string))  
        );  
  
  
  
        //echo curl_exec($ch);  
        ob_start();  
        echo curl_exec($ch);  
        if (curl_errno($ch)) {  
  
            $this->ErrorLogger('curl falied. Error Info: ' . curl_error($ch));  
  
        }  
        $return_content = ob_get_contents();  
        ob_end_clean();  
  
        //exit();  
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //注意curl_getinfo 要在curl_exec 後面運行,不然都返回0  
        //echo $return_code;  
        return array($return_code, $return_content);  
    }  
  
    //下載二維碼到服務器  
    protected function DownLoadQr($url, $filestring)  
    {  
  
        echo '二維碼 url' . $url;  
        if ($url == "") {  
  
            return false;  
  
        }  
  
        $filename = $filestring . '.jpg';  
  
        //ob_start();  
        //echo readfile($url);  
        //$img = ob_get_contents();  
        //ob_end_clean();  
        //$size = strlen($img);  
        $img = $this->curl_get_contents($url);  
        $size = strlen($img);  
        $fp2 = fopen('./' . $filename, "a");  
  
        if (fwrite($fp2, $img) === false) {  
  
            $this->ErrorLogger('dolwload image falied. Error Info: 沒法寫入圖片');  
  
            exit();  
  
        }  
  
        fclose($fp2);  
  
        return './' . $filename;   //./Uploads/qrcode/  
  
    }  
  
    private function ErrorLogger($errMsg)  
    {  
  
        $logger = fopen('./ErrorLog.txt', 'a+');  
  
        fwrite($logger, date('Y-m-d H:i:s') . " Error Info : " . $errMsg . "rn");  
  
    }  
  
    protected function curl_get_contents($url)  
    {  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);  
        curl_setopt($ch, CURLOPT_USERAGENT, '');  
        curl_setopt($ch, CURLOPT_REFERER,'');  
        @curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
        $r = curl_exec($ch);  
        curl_close($ch);  
        return $r;  
    }  
  
  
  
}  
  
// 演示代碼  
  
$weixinerwe = new Weixin();  
echo  $weixinerwe->getEwm(  
    array('appid'=>'','secret'=>'' ),  
    10000);  
  
  
//http://weixin.qq.com/q/aj8LDoLm9L_NGznHoxOY  
  
  
  
  
  
/* 
 * 
 * 1、場景二維碼 
爲了知足用戶渠道推廣分析的須要,公衆平臺提供了生成帶參數二維碼的接口。使用該接口能夠得到多個帶不一樣場景值的二維碼,用戶掃描後,公衆號能夠接收到事件推送。 
目前有2種類型的二維碼,分別是臨時二維碼和永久二維碼,前者有過時時間,最大爲1800秒,但可以生成較多數量,後者無過時時間,數量較少(目前參數只支持1--100000)。兩種二維碼分別適用於賬號綁定、用戶來源統計等場景。 
用戶掃描帶場景值二維碼時,可能推送如下兩種事件: 
若是用戶還未關注公衆號,則用戶能夠關注公衆號,關注後微信會將帶場景值關注事件推送給開發者。 
若是用戶已經關注公衆號,在用戶掃描後會自動進入會話,微信也會將帶場景值掃描事件推送給開發者。 
獲取帶參數的二維碼的過程包括兩步,首先建立二維碼ticket,而後憑藉ticket到指定URL換取二維碼。 
 
2、建立二維碼ticket 
每次建立二維碼ticket須要提供一個開發者自行設定的參數(scene_id),分別介紹臨時二維碼和永久二維碼的建立二維碼ticket過程。 
臨時二維碼請求說明 
http請求URL 
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN 
POST數據格式 
 
1 { 
2 "expire_seconds": 1800, 
3 "action_name": "QR_SCENE", 
4 "action_info": { 
5 "scene": { 
6 "scene_id": 100000 
7 } 
8 } 
9 } 
 
返回格式: 
{ 
"ticket": "gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA==", 
"expire_seconds": 1800 
} 
 
永久二維碼請求說明 
http請求URL 
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN 
POST數據格式 
 
{ 
"action_name": "QR_LIMIT_SCENE", 
"action_info": { 
"scene": { 
"scene_id": 1000 
} 
} 
} 
 
返回格式 
{ 
"ticket": "gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==" 
} 
 
提交數據參數說明: 
 
返回結果參數說明 
 
程序實現 
 
3、經過ticket換取二維碼 
獲取二維碼ticket後,開發者可用ticket換取二維碼圖片。無須登陸態便可調用。 
HTTPS GET請求說明(TICKET必需UrlEncode) 
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET 
ticket正確狀況下,http 返回碼是200,是一張圖片,能夠直接展現或者下載。 
HTTP頭示例以下: 
 
1 { 
2 "url": "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA%3D%3D", 
3 "content_type": "image/jpg", 
4 "http_code": 200, 
5 "header_size": 162, 
6 "request_size": 181, 
7 "filetime": -1, 
8 "ssl_verify_result": 20, 
9 "redirect_count": 0, 
10 "total_time": 0.509, 
11 "namelookup_time": 0, 
12 "connect_time": 0.058, 
13 "pretransfer_time": 0.343, 
14 "size_upload": 0, 
15 "size_download": 28497, 
16 "speed_download": 55986, 
17 "speed_upload": 0, 
18 "download_content_length": 28497, 
19 "upload_content_length": 0, 
20 "starttransfer_time": 0.481, 
21 "redirect_time": 0 
22 } 
 
4、掃描帶參數二維碼事件 
用戶掃描帶場景值二維碼時,可能推送如下兩種事件: 
若是用戶還未關注公衆號,則用戶能夠關注公衆號,關注後微信會將帶場景值關注事件推送給開發者。 
若是用戶已經關注公衆號,則微信會將帶場景值掃描事件推送給開發者。 
1. 用戶未關注時,進行關注後的事件推送 
 
<xml> 
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName> 
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName> 
<CreateTime>1389684286</CreateTime> 
<MsgType><![CDATA[event]]></MsgType> 
<Event><![CDATA[subscribe]]></Event> 
<EventKey><![CDATA[qrscene_1000]]></EventKey> 
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket> 
</xml> 
 
參數說明 
 
2. 用戶已關注時的事件推送 
 
<xml> 
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName> 
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName> 
<CreateTime>1389684184</CreateTime> 
<MsgType><![CDATA[event]]></MsgType> 
<Event><![CDATA[SCAN]]></Event> 
<EventKey><![CDATA[1000]]></EventKey> 
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket> 
</xml> 
 
參數說明 
 
事件代碼: 
如下代碼斷定了掃描帶參數二維碼的兩種情形 
 
private function receiveEvent($object) 
{ 
$contentStr = ""; 
switch ($object->Event) 
{ 
case "subscribe": 
$contentStr = "歡迎關注方倍工做室 "; 
if (isset($object->EventKey)){ 
$contentStr = "關注二維碼場景 ".$object->EventKey; 
} 
break; 
case "SCAN": 
$contentStr = "掃描 ".$object->EventKey; 
//要實現統計分析,則須要掃描事件寫入數據庫,這裏能夠記錄 EventKey及用戶OpenID,掃描時間 
break; 
default: 
break; 
 
} 
$resultStr = $this->transmitText($object, $contentStr); 
return $resultStr; 
} 
 
若是您不懂微信第三方開發,仍是找人作吧 
 * 
 * */  
相關文章
相關標籤/搜索