欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

微信JSAPI 微信内置JSAPI 2015年1月官方正式API接口,分享完整实

程序员文章站 2022-03-24 22:27:16
...

微信JSAPI微信内置JSAPI2015年1月官方正式API接口,分享完整实例 2015年1月30日,今天发现一些JS分享问题,一直很费解,通过查看原版开发手册,发现了根据原版的JSSDK进行修改补充。 主要用于微信浏览器分享朋友圈,转发好友等功能。 碎片赚钱哪家强,快来微

微信JSAPI 微信内置JSAPI 2015年1月官方正式API接口,分享完整实例
2015年1月30日,今天发现一些JS分享问题,一直很费解,通过查看原版开发手册,发现了根据原版的JSSDK进行修改补充。
主要用于微信浏览器分享朋友圈,转发好友等功能。
碎片赚钱哪家强,快来微信找微饷。关注《微饷》微信公众账号。
QQ:2444756311
GetSignPackage();
?>


appId = $appId;
    $this->appSecret = $appSecret;
    $this->User_ID = $User_ID;
  }
  
  public function Ext_FileSystem(){
    if(!file_exists("jSon_file/jsapi_ticket_".$this->User_ID.".json")){
        touch("jSon_file/jsapi_ticket_".$this->User_ID.".json");
        chmod("jSon_file/jsapi_ticket_".$this->User_ID.".json",0755);
    }
    
    if(!file_exists("jSon_file/access_token_".$this->User_ID.".json")){
        touch("jSon_file/access_token_".$this->User_ID.".json");
        chmod("jSon_file/access_token_".$this->User_ID.".json",0755);
    }
  }
  
  public function getSignPackage() {
    $this->Ext_FileSystem();
    $jsapiTicket = $this->getJsApiTicket();
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $timestamp = time();
    $nonceStr = $this->createNonceStr();

    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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 User_ID.".json"));
    if ($data->expire_time 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("jSon_file/jsapi_ticket_".$this->User_ID.".json", "w");
        fwrite($fp, json_encode($data));
        fclose($fp);
        chmod("jSon_file/jsapi_ticket_".$this->User_ID.".json",0777);
      }
    } else {
      $ticket = $data->jsapi_ticket;
    }

    return $ticket;
  }

  private function getAccessToken() {
    // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
    $data = json_decode(file_get_contents("jSon_file/access_token_".$this->User_ID.".json"));
    if ($data->expire_time 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("jSon_file/access_token_".$this->User_ID.".json", "w");
        fwrite($fp, json_encode($data));
        fclose($fp);
        chmod("jSon_file/access_token_".$this->User_ID.".json",0777);
      }
    } else {
      $access_token = $data->access_token;
    }
    return $access_token;
  }

  private function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_URL, $url);

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

    return $res;
  }
}
appId = $appId;
    $this->appSecret = $appSecret;
  }

  public function getSignPackage() {
    $jsapiTicket = $this->getJsApiTicket();
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $timestamp = time();
    $nonceStr = $this->createNonceStr();

    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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 expire_time 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("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("access_token.json"));
    if ($data->expire_time 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("access_token.json", "w");
        fwrite($fp, json_encode($data));
        fclose($fp);
      }
    } else {
      $access_token = $data->access_token;
    }
    return $access_token;
  }

  private function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_URL, $url);

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

    return $res;
  }
}