?? WeChat PC ? ???? ?? WeChat ?? ??? ???? ????. ???? ???? ?? WeChat ?? ??? ??? ??? ????? ???? ???? ???? ????? WeChat? ?????? ???? ?????. ?? ?? ???? ??? ??? ??? ????? ???? WeChat QR ??? ???? ???? ??? ?? ??? ????. ???? ?? ???? ??? ??? ???? ?? ??? ??? ???????.
?? ???? ?? ??? ??? ???? ????.
1 ??
?? ?? ?? ??? ?????-> ?? ??
??? ?? ?? ??????. URL? ?? ??? ?????. ? ??? thinkPHP ?????? ???? ????? ??? ????. ??(??)? ? ???? ?????(?: WechatAction.class.php). ??? Action?? ? ?? ???? ?????(?: URLRedirect()). ??? URL? http://[IP]:[port]/index.php/Decoration/Wechat/UrlRedirect???. ?? ?? ??? ???? ??? ?? ??? ?????. EncodingAESKey ?? ??? ?? ?? ??? ???? WeChat? ? URL? ?????. ?? ????? ??? ???? ??? ??? ??? WeChat ???? ???? ????? ??? ? ????. ?? ??? ??? ????. ??? ???? ??? get ??? ?????. ??? ??? return? ???, thinkPHP? ???? ???? echo? ???? ???. I('echostr'); ?????. ??? ????? ??? ??? ????.
2 ????? ?? QR??? ??
WeChat?? ????? ??? QR??? ??? ??? ?? QR???? ?? ??? ?? QR?????. ??? ??? ??? ? ?? ?? QR?? ??? ??? ????. ??? ??? ???? ??? ?? ?????? ??? ??? ? ??? ?? ????. ?? ?? ?? ??? ?? ??? ??? ?? ??? ??? ??? ??? ??? ???? ?? ? ?????? ? ? ????. QR ??? ???? ???? WeChat? ????? ?? ???. ? QR ??? ???? ?? ?? ???? ??? ???? ??? ? ?? ?? ????? ??? ???? ?????. ?? ?? ??? ???? ???? ???? ?? ??? ?? ???? ??? ? ????. ??? ?? QR??? ????? ??? ?????.
?? ?? ???? ?????. ?? ???? ??? ?????.
???? ? QR ??? ????, ???? ?? ??? ???? ?, ???? ?? ???? ?? ???? ?? ???? ???. WeChat ??? ?? ???? ??? ?? URL? ???? ????, ???? ??? ?? ????? ??? ? ????. ???? ?? ??? ????? ?? ?? ?? ?? ?? ??? ???? ?????. ???? ???? ???? ?? ??? ?? ???? ?? ???? ???. ?? WeChat ??? ???????. ??? ??? URL? ??? ???? ???? ????? ??? ???? ? ????? ??? ??? ???? ?? ??? ??? ? ????.
3 ???? ?? ??
3.1 access_token ??
? access_token? WeChat ?????? ???? ?? ????? ??????. ?? ?? ??? 7200???? ????? access_token? ?????? ???.
?? ??:
方法?:?GET url?:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
???? APPID ? APPSECRET? ?? ??? APPID ? APPSECRET?? WeChat ?? ???? ?? ? ????.-> ?? ????? ??? ???? ?? JSON ???? ?????.
{"access_token":"ACCESS_TOKEN","expires_in":7200}
??? access_token? ?? ????? ?? ???? ??_in? ?? ?? ?????.
?? ????? access_token? ??????? ???? ?? ??? ??? ?? ?? ?? getWechatAccessToken()? ??????. access_token? ???????. ??? ?? ?? ????, ??? ??? ??????? ??? access_token? ??????. ? access_token? ?? ?? ? ?? ??? ????? ???. ??? getWechatAccessToken()? ???? ?????.
?//獲取access_token function?getWechatAccessToken(){ ????$wechatInfo?=?M('wechat_info')->select(); ????$wechatInfo?=?array_reduce($wechatInfo,?create_function('$result,?$v',?'$result[$v["conf_name"]]?=?$v;return?$result;')); ????$expireTime?=?$wechatInfo['PUBLIC_WECHAT_ACCESSTOKEN_EXPIRES']['conf_value'];?????????????????????????????//前面不用管,是我數(shù)據(jù)庫相應(yīng)設(shè)置 ????if?(time()?< $expireTime){ //access_token未過期 return $wechatInfo['PUBLIC_WECHAT_ACCESSTOKEN']['conf_value']; }else{ //access_token過期,重新獲取 $baseUrl = C('WECHAT_PUBLIC_GET_ACCESS_TOKEN'); $url = str_replace("##APPSECRET##", $wechatInfo['PUBLIC_WECHAT_APPSECRET']['conf_value'], str_replace("##APPID##", $wechatInfo['PUBLIC_WECHAT_APPID']['conf_value'], $baseUrl)); $result = file_get_contents($url); $result = json_decode($result, true); if (array_key_exists('errorcode', $result)){ //失敗重試一次 return false; }else{ M('wechat_info')->where(array('conf_name'?=>?'PUBLIC_WECHAT_ACCESSTOKEN'))->save(array('conf_value'?=>?$result['access_token'])); ????????????M('wechat_info')->where(array('conf_name'?=>?'PUBLIC_WECHAT_ACCESSTOKEN_EXPIRES'))->save(array('conf_value'?=>?time()+$result['expires_in']-200)); ????????????return?$result['access_token']; ????????} ????} }rrree
?? ???? ??? ?? ???? ??? ? ????.
3.2 ?? QR?? ??
3.2.1 ?? ??
C('WECHAT_PUBLIC_GET_ACCESS_TOKEN')?=?https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
????? URL? TOKEN? 3.1?? ?? access_token???. ??? ???? ??_?? QR ??? ?? ????, action_name? ??? ?? ?? 30????. QR ??? QR_SCENE?? ???? ??? scene_id? ??? ?? ?????? 0? ?? 32?? ?????. ???????? ?? ID? ??????. WeChat ??? ???? ???? ?????. ? ?? ??? ??? ?????? ??? ?? ?? ??? ????. ?? ?? ???? ???? ????? ???? ?? ??? ??????.
??? ?? QR ??? ???? ???? ?????.
??請(qǐng)求方式:?POST ?????接口:https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN ????????POST數(shù)據(jù):?{"expire_seconds":?604800,?"action_name":?"QR_SCENE",?"action_info":?{"scene":?{"scene_id":?123}}}
api_notice_increment() ???? I???. ?? POST ???? ????? WeChat ?????? POST ???? ????? ?? ??? ??? ??? ??? ??? ?? ?????. ??? ????? ??? ? ?? ???? POST ???? ?????. ?? ?? ??? ???. WeChat?? ??? ???? ??? ?????. ??? WeChat ?????? ???? ? postman? ???? ?? ??? ????? ????? ?? ??? JSON ?????? ???. . ?. ?? ???? ??? ????.
//創(chuàng)建臨時(shí)二維碼 function?getTemporaryQrcode($orderId){ ????$accessToken?=?getWechatAccessToken(); ????$url?=?str_replace("##TOKEN##",?$accessToken,?C('WECHAT_PUBLIC_GET_TEMPORARY_TICKET')); ????$qrcode?=?'{"expire_seconds":?1800,?"action_name":?"QR_SCENE",?"action_info":?{"scene":?{"scene_id":?'.$orderId.'}}}'; ????$result?=?api_notice_increment($url,?$qrcode); ????$result?=?json_decode($result,?true); ????return?urldecode($result['url']); }
getTemporaryQrcode() ?? ???? ?? ??? ? ? ?? ????? ???, ?? ??? WeChat ????? ?????.
function?api_notice_increment($url,?$data){????$ch?=?curl_init();????$header?=?"Accept-Charset:?utf-8"; ????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; ????} }
? ?????? ?? ?? ??? ????.
C('WECHAT_PUBLIC_GET_TEMPORARY_TICKET')?=?https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=##TOKEN##
ticket? ?? ??? ???? ?????,expire_seconds? QR ?????. ????, URL? ??? QR??? ??? ? ??? ?????. ??? QR ??? ???? ??? ?? ???? ?? ??? ??? ??? ????. ? ???? ??? url ?? ?? ??? ?? ? url ?? ???? QR ??? ?????. ??? ??? ? ????. phpqrcode? ???? PHP?? QR ??? ??? ? ???, ?? ???? ?? ????. ?? ??? ???? ???? ????.
3.2.2 QR ?? ?? ??
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==","expire_seconds":60,"url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"}
? ?????? ?? ?? ?? ????? ????? ? ?? ?????. ??? ??? ?? ??? ?? ??? ??????.
3.3 ???? QR ??? ??? ? ???? ??
3.3.1 掃描后發(fā)生了什么
上面提到了,用戶掃描我們生成的臨時(shí)二維碼,如果用戶未關(guān)注公眾號(hào),則首先會(huì)跳轉(zhuǎn)到公眾號(hào)的關(guān)注頁面,點(diǎn)擊關(guān)注后,會(huì)進(jìn)入公眾號(hào)的會(huì)話頁面,同時(shí)會(huì)給我們?cè)O(shè)置的接口推送一個(gè)事件。如果用戶已經(jīng)關(guān)注了,用戶微信會(huì)直接跳轉(zhuǎn)到公眾號(hào)會(huì)話頁面,然后微信服務(wù)器會(huì)給我們?cè)O(shè)置的接口推送一個(gè)事件。
用戶關(guān)注與否微信服務(wù)器給我們推送的事件是差不多的,只是新關(guān)注用戶推送的事件中scene_id前面會(huì)加一個(gè)前綴。下面是微信公眾平臺(tái)文檔的說明:
用戶未關(guān)注時(shí),進(jìn)行關(guān)注后的事件推送
<xml><ToUserName><![CDATA[toUser]]></ToUserName> //開發(fā)者微信號(hào)<FromUserName><![CDATA[FromUser]]></FromUserName> //發(fā)送者賬號(hào)(openid)<CreateTime>123456789</CreateTime> //消息創(chuàng)建時(shí)間(整型)<MsgType><![CDATA[event]]></MsgType> //消息類型?event<Event><![CDATA[subscribe]]></Event> //事件類型(subscribe)<EventKey><![CDATA[qrscene_123123]]></EventKey> //事件KEY值,qrscene_為前綴,后面為二維碼參數(shù)值<Ticket><![CDATA[TICKET]]></Ticket> //二維碼ticke值,可以用來換取二維碼圖片</xml>
? 用戶已關(guān)注時(shí)的事件推送
?<xml><ToUserName><![CDATA[toUser]]></ToUserName> ?//開發(fā)者微信號(hào)<FromUserName><![CDATA[FromUser]]></FromUserName> ? ?//發(fā)送者賬號(hào)(openid)<CreateTime>123456789</CreateTime> ?//消息創(chuàng)建時(shí)間<MsgType><![CDATA[event]]></MsgType>???????????????????? ?//消息類型event<Event><![CDATA[SCAN]]></Event> ? ?//事件類型?event<EventKey><![CDATA[SCENE_VALUE]]></EventKey>???????????? ?//事件key值,是一個(gè)32位無符號(hào)整數(shù),即創(chuàng)建二維碼時(shí)的二維碼scene_id<Ticket><![CDATA[TICKET]]></Ticket>????????????????????? ?//二維碼的ticke,可以用來換取二維碼圖片</xml>
?
3.3.2 我們要做些什么
我們需要在自己填寫的URL接口中接收這個(gè)事件,然后拿到我們需要的東西做我們想干的事兒。因?yàn)槲乙獙?shí)現(xiàn)的功能比較簡單,只需要拿到scene_id即可,因?yàn)檫@是我要展示給用戶看的訂單數(shù)據(jù)。下面是我寫的接收和處理部分,比較簡單,主要看一下應(yīng)該怎么接收微信推送的事件:
public?function?urlRedirect(){???????? $postStr?=?$GLOBALS["HTTP_RAW_POST_DATA"];???????? $postObj?=?simplexml_load_string($postStr,?'SimpleXMLElement',?LIBXML_NOCDATA);???????? $fromUsername?=?(string)$postObj->FromUserName;???????? $EventKey?=?trim((string)$postObj->EventKey);???????? $keyArray?=?explode("_",?$EventKey);???????? if?(count($keyArray)?==?1) {????????? //已關(guān)注者掃描 $this->sendMessage($fromUsername,?$EventKey); } else { //未關(guān)注者關(guān)注后推送事件 $this->sendMessage($fromUsername,?$keyArray[1]); ????????} ????}
我沒有使用其他參數(shù),只是根據(jù)不同的推送事件拿到我想要的訂單ID,然后這時(shí)候其實(shí)相當(dāng)于你在這里用公眾號(hào)的客服在跟掃碼的這個(gè)用戶對(duì)話,上段代碼中調(diào)用的sendMessage()是使用客戶賬號(hào)給掃碼用戶發(fā)送一個(gè)圖文消息,因?yàn)槲以谀胹cen_id的同時(shí)也拿到了用戶的openid,可以利用這個(gè)給用戶發(fā)送消息。
下面是?sendMessage()?方法:
//給用戶發(fā)送圖文消息,點(diǎn)擊跳轉(zhuǎn)到報(bào)價(jià)頁面 ????public?function?sendMessage($openid,$orderId){???????? ????$url?=?str_replace('##TOKEN##',?getWechatAccessToken(),?C('WECHAT_SEND_MESSAGE'));???????? ????$redirectUrl?=?str_replace("##ORDERID##",?$orderId,?str_replace("##OPENID##",?$openid,?C('WECHAT_REDIRECT_URL_PRE')));???????? ????$orderInfo?=?M('order')->where(array('orderid'?=>?$orderId))->field(array('totalMoney',?'savedMoney',?'roomarea'))->find();???????? ????$description?=?str_replace("##ROOMAREA##",?intval($orderInfo['roomarea']?*?1.25),?C('WECHAT_MESSAGE_BRIEF'));???????? ????$description?=?str_replace("##TOTALBUDGET##",?$orderInfo['totalMoney'],?$description);???????? ????$description?=?str_replace("##MARKETBUDGET##",?$orderInfo['totalMoney']+$orderInfo['savedMoney'],?$description);???????? ????$description?=?str_replace("##SAVEMONEY##",?$orderInfo['savedMoney'],?$description);???????? ????$dataStr?=?'{"touser":"'?.?$openid?.?'","msgtype":"news","news":{"articles":[{"title":"'?.?C('WECHAT_MESSAGE_TITLE')?. ????????????'","description":"'?.?$description?.?'","url":"'?.?$redirectUrl?.?'","picurl":"'?.?C('WECHAT_MESSAGE_PICURL')?.?'""}]}}'; ????????api_notice_increment($url,?$dataStr); ????}
其中? C('WECHAT_SEND_MESSAGE') = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=##TOKEN##' ?至于下面的一大段str_replace,就是在組給用戶發(fā)送的文字而已,需要注意$dataStr的格式,這里面要求JSON字符串比較嚴(yán)格,必須所有的字符串都用雙引號(hào)括起來。微信接口對(duì)POST參數(shù)的限制真心嚴(yán)格。
下面是微信公眾平臺(tái)開發(fā)者文檔中要求發(fā)送圖文消息的POST data格式:
{ ????"touser":"OPENID", ????"msgtype":"news", ????"news":{ ????????"articles":?[ ?????????{ ?????????????"title":"Happy?Day", ?????????????"description":"Is?Really?A?Happy?Day", ?????????????"url":"URL", ?????????????"picurl":"PIC_URL" ?????????}, ?????????{ ?????????????"title":"Happy?Day", ?????????????"description":"Is?Really?A?Happy?Day", ?????????????"url":"URL", ?????????????"picurl":"PIC_URL" ?????????} ?????????] ????} }
其中url是用戶點(diǎn)擊這個(gè)消息之后打開的地址,這個(gè)時(shí)候我就組了一個(gè)自己網(wǎng)站的地址,是一個(gè)get請(qǐng)求地址,里面攜帶參數(shù)是用戶的openid和訂單id,這樣用戶點(diǎn)擊開圖文消息就可以看到自己剛才下單的內(nèi)容了,因?yàn)樾枰诰W(wǎng)頁上展示用戶的微信頭像和昵稱,所以我把openid也放到參數(shù)里,在頁面加載前先拿到用戶的個(gè)人信息和訂單數(shù)據(jù),再展示網(wǎng)頁。這樣流程:用戶未登錄下單 -> 生成微信二維碼 -> 用戶掃碼關(guān)注公眾號(hào) -> 查看訂單詳細(xì)信息?就完成了。而且因?yàn)檫@個(gè)圖文消息打開后的鏈接攜帶的參數(shù)是這個(gè)用戶的額openid和其下單的訂單ID,不管分享到哪兒,用什么瀏覽器打開都是可以訪問的,且展示的也是這個(gè)用戶的頭像和昵稱信息,這也是我要實(shí)現(xiàn)的一個(gè)效果。
更多微信開發(fā)—帶參數(shù)二維碼的使用?相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP? ? ?? ? ?? ? ?????, ?? WeChat ??? ?? ???? ?? ?? ???? ?????. ??? ?? ? ?? ??? ???? WeChat ??? PHP? ???? ???? ????. PHP? ??? ?? ???? ?? ?? ???? ?????. WeChat ???? ??? ??? ? ???? ??? ??? ???? ?? ??? ?? ??? ?????. ??? ? ??? ??? ?? ???? ?? ??? ?? ???? ??? ? ?? ????? ??? ? ? ????.

WeChat ?? ??? ??? ? ?? ??? ?? ?????. ?? ??? ????? ?? ??? ??? ??? ? ?? ?? ????, ??? ?? ? ?? ??? ?? ??? ????? ???. ? ????? PHP? ???? WeChat ?? ??? ???? ??? ?????. WeChat ?? ?? ??? ???? ?? WeChat ?? ?? ??? ??? ???. WeChat ?? ?????? WeChat ?? ??, ?? ?? ? ?? ??? ???? ??? API ??? ???? ???. PHP ??? ???? ???? ???? WeChat?? ????? ???? PH? ???? ???.

WeChat? ??? ?? ?? ? ?? ??? WeChat? ??? ??? ???? ??????. WeChat ?? ??? ??? ??? WeChat ???? ???? ??? ?? ? ?????. ??? ?? ???? ????? ??? ?????? ?? ??? ?? ??? ?? ?????. ??? WeChat ?? ??? ??? ???? ?? ?? ?????. ? ????? PHP? ???? WeChat ?? ??? ??? ???? ??? ?????. 1. ?? ?? WeChat ?? ??? ??? ????? ?? ?? ??? ???? ???. PHP WeChat ?? ??? ??? ?? ?? ?? ?? ??: Sub

WeChat? ?? ???? ?? ? ??? ??? ??? ?? ??? ? ?????. ??? ???? ??? ?? ?? ? ?? ???? WeChat ???? ???? ??? ??????. WeChat ???? ??? ? ?? ???? ??? ?????. ?? ??? ?? ?? ? ? ???? ?? WeChat ??? PHP ??? ??? ? ????. 1. PHP ?? WeChat ?? PHP? ? ?? ???? ?? ???? ?? ?? ?? ? ???? ?????. WeChat ?? ????? ???? ?? ?????? ???? PHP ??? ???? WeChat? ??? ? ????.

WeChat ?? ?? ???? ??? ?? ??? ???? ???? ? ? ???? ??? ? ??? ?? ?? ??? ?????. ? ????? PHP? ???? WeChat ??? ?? ?? ??? ???? ??? ?????. 1. WeChat ???? openid? ?????. WeChat ??? ?? ?? ??? ???? ?? ?? ???? openid? ???? ???. WeChat ?? ??? ??? ? ??? ??? ?? openid? ?? ?? ???? ?????. ??? ??? ???? ?? ??? ?? ???? ?? ? ????.

WeChat? ???? ??? ?? ? ??? ?????? ??? ???, WeChat? ??? ??? ??? ?? ??? ??? ??? ??? ?? ????. ??? ?? WeChat? ??? ????? ???? ?? ??? ??? ???? WeChat ??? ???? ?? ?? ???? ????. ? ? ?? ?? ??? ?? ?? ?????. ???? PHP ??????? ?? ??? ?? ??? ??? ???? ???? ??? ??? ?????. 1. WeChat ?? ??? ??? ?? ??? ?????. ?? ??? ?? ??? ???? ??? ?????.

PHP? ???? WeChat ?? ??? ???? ?? WeChat ?? ??? ?? ??? ?? ? ?? ??? ?? ??? ??? ????, ????? ???? ? ??? PHP? ???? WeChat ?? ??? ??? ?? ????. ? ????? PHP? ???? WeChat ?? ??? ???? ???? ??? ?????. 1??: WeChat ?? ??? ??? ??? ????. WeChat ?? ?? ??? ???? ?? WeChat ?? ??? ??? ??? ???? ???. ???? ?? ??? WeChat ?? ??? ?? ????? ?????.

???? ??? ??? ??? ???? WeChat? ?? ? ??? ???? ???? ?? ??? ?????. ?? ? ?????? ??? WeChat ??? PHP? ???? ??? ?? ???? ??? ?????. ? ????? ?? WeChat ??? PHP? ???? ??? ?? ?? ?? ???? ?? ? ? ?? ??? ?????. 1. ?? ?? ?? WeChat? ???? ?? ?? ?? ?? ??? ???? ???. ??, PHP ?? ??? WeChat ?? ???? ???? ???.
