码迷,mamicode.com
首页 > Windows程序 > 详细

js_sdk api

时间:2015-02-11 18:04:33      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

<?php
    header("Content-type: text/html; charset=utf-8");

    if($_GET[‘appId‘] && $_GET[‘appSecret‘]){
        $appId=$_GET[‘appId‘] ;
        $appSecret=$_GET[‘appSecret‘];
    }else{
        echo "请输入appid和appSecret";
        exit;
    }
    $jsapiTicket = getJsApiTicket($appId,$appSecret);
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $timestamp = time();
    $nonceStr = createNonceStr();
    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&amp;timestamp=$timestamp&url=$url";
    $signature = sha1($string);
    $signPackage = array(
      "appId"     => $appId,
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "url"       => $url,
      "signature" => $signature,
      "rawString" => $string
    );
    $data_json=json_encode($signPackage,true);
    echo $data_json;



  function getJsApiTicket($appId,$appSecret) {
    $memcache_obj = memcache_connect("localhost", 11211);
    $jsapi_ticket=$memcache_obj->get(‘jsapi_ticket‘);
    if(!$jsapi_ticket){
      $accessToken = getAccessToken($appId,$appSecret);
      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
      $res = json_decode(httpGet($url));
      $jsapi_ticket = $res->ticket;
      if ($jsapi_ticket) {
          $memcache_obj->set(‘jsapi_ticket‘, $jsapi_ticket, false, 7000);
      }
    }
    return $jsapi_ticket;
  }


  function getAccessToken($appId,$appSecret) {
    $memcache_obj = memcache_connect("localhost", 11211);
    $access_token=$memcache_obj->get(‘access_token‘);
    if(!$access_token){
       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
      $res = json_decode(httpGet($url));
       $access_token = $res->access_token;

      if ($access_token) {
          $memcache_obj->set(‘access_token‘, $ticket, false, 7000);
      }
    }
    return $access_token;
  }




  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;
  }
  function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
  }

 

js_sdk api

标签:

原文地址:http://www.cnblogs.com/yutaoyao/p/4286448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!