html5 微信扫一扫功能实现

 html 





    
    
    












 

php获取签名   

public function sys(){
    $appid = '';
    $secret = '';
    //这里获取access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
    $result = $this->https_request($url);
    $json1 = json_decode($result,true);
 
    //通过access_token获取ticket
    $us = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?    access_token=".$json1['access_token']."&type=jsapi";
    $resu = $this->https_request($us);
    $json = json_decode($resu,true);

    //生成签名的时间戳
    $timestamp = time(); 

    //生成16位随机字符串
    $nonceStr = $this->createNonceStr();

    //获取当前url
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $string = "jsapi_ticket=".$json['ticket']."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url."";

    //最重要的我也不知道叫什么
    $signature = sha1($string);

    $signPackage = array(
        "appId"=>$appid,
        "nonceStr"=>$nonceStr,
        "timestamp"=>$timestamp,
        "url"=>$url,
        "signature"=>$signature,
        "rawString"=>$string
    );

    return $signPackage;
}

//zifuchuan
public 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;
}

//回调
public function https_request($url,$data=null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);

    return $output;
}

 

你可能感兴趣的:(html5 微信扫一扫功能实现)