您好,登錄后才能下訂單哦!
這篇文章主要介紹yii如何調用微信接口掃二維碼,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
<?php /** * desc:微信js調用類 * author:besttaowenjing@163.com * date:2016-07-04 */ namespace application\vendors\weixin; class Jssdk { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage(){ $jsapiTicket = $this->getJsApiTicket(); $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function getJsApiTicket() { // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例 $data = json_decode(file_get_contents(dirname(__FILE__).'/jsapi_ticket.json')); //$data = (object)array(); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen(dirname(__FILE__)."/jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例 $data = json_decode(file_get_contents(dirname(__FILE__)."/access_token.json")); //$data = (object)array(); if ($data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen(dirname(__FILE__)."/access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } function httpGet($url) { $ch = curl_init(); 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_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); $temp = curl_exec($ch); curl_close($ch); return $temp; } } ?> <?php /** * desc:h6簽到相關 * author:besttaowenjing@163.com * date:2016-06-22 */ use application\vendors\weixin\Jssdk; class SignController extends Controller { private $appid = 'xxx'; private $appsecret = 'xxx'; /** * desc:得到活動詳情 */ public function actionGet_activity() { $request = Yii::app()->request; $userId = intval($request->getParam('userid')); if ($userId < 1) { echo '驗證失敗'; exit; } $activityId = intval($request->getParam('activityid')); if ($activityId < 1) { echo '驗證失敗'; exit; } $criteria = new CDbCriteria; $criteria->select = 'id, title, address, starttime, num'; $activity = Activity::model()->findbypk($activityId, $criteria); if (empty($activity)) { echo '您所查詢的數據不存在';exit; } $jssdk = new Jssdk($this->appid, $this->appsecret); $signPackage = $jssdk->getSignPackage(); $data = array(); $token = md5(microtime(true)); Yii::app()->session['signtoken'] = $token; $data['userid'] = $userId; $data['activity'] = $activity; $data['token'] = $token; $data['signPackage'] = $signPackage; $this->renderPartial('sign', $data); } /** * desc:json輸出 */ private function _showMsg($data) { echo json_encode($data); exit; } }
以上是“yii如何調用微信接口掃二維碼”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。