后端开发

网络新概念,云计算、大数据、O2O、电商。。。。

一、登录微信企业号设置设置环境:

应用中心新建应用主页型应用输入如下内容提交保存;然后设置可信任域名,主页URL就是你开发的程序入口

js01.jpg

设置权限管理新建下一步设置应用权限点击修改(增加新建的应用)

js02.jpg

然后可以看到corpidsecretid这两个重要的参数,在订阅号和服务号中获取这两个参数好像简单点;


二、PHP开发代码:

<?php

$appId = 'CorpID';//根据管理组中的信息设置

$appsecret = 'Secret';//根据管理组中的信息设置


$timestamp = time();

$jsapi_ticket = make_ticket($appId,$appsecret);

$nonceStr = make_nonceStr();

$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$signature = make_signature($nonceStr,$timestamp,$jsapi_ticket,$url);


function make_nonceStr()

{

$codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

for ($i = 0; $i<16; $i++) {

$codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];

}

$nonceStr = implode($codes);

return $nonceStr;

}


function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)

{

$tmpArr = array(

'noncestr' => $nonceStr,

'timestamp' => $timestamp,

'jsapi_ticket' => $jsapi_ticket,

'url' => $url

);

ksort($tmpArr, SORT_STRING);

$string1 = http_build_query( $tmpArr );

$string1 = urldecode( $string1 );

$signature = sha1( $string1 );

return $signature;

}


function make_ticket($appId,$appsecret)

{

// access_token 应该全局存储与更新,以下代码以写入到文件中做示例

$data = json_decode(file_get_contents("access_token.json"));

if ($data->expire_time < time()) {

//$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;

$TOKEN_URL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$appId."&corpsecret=".$appsecret;//这是企业号的地址与订阅号不同

$json = file_get_contents($TOKEN_URL);

$result = json_decode($json,true);

$access_token = $result['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;

}


// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例

$data = json_decode(file_get_contents("jsapi_ticket.json"));

if ($data->expire_time < time()) {

//$ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";

$ticket_URL="https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=".$access_token;//这是企业号的地址与订阅号不同

$json = file_get_contents($ticket_URL);

$result = json_decode($json,true);

$ticket = $result['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;

?>

三、完整代码下载:

链接: http://pan.baidu.com/s/1eRHDvuM   密码: wgu3




TAG: 开发