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:h5签到相关
 * 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调用微信接口扫二维码

上一篇:Android微信自动回复功能


下一篇:《众媒时代》:2015年美国传统媒体在移动时代的困惑、冲击与转型,最遗憾的是没有对微信上的信息传播情况的全面的概括分析。三星推荐