<?php
$appid = "wx0f412c1bcb393318";
$secret = "5c19f93471ff857885a67813c67220ec";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$res = file_get_contents($url);
$data = json_decode($res);
var_dump($data->access_token);
$jsapi_ticket_json = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$data->access_token.'&type=jsapi');
$jsapi_ticket_obj = json_decode($jsapi_ticket_json);
$jsapi_ticket = $jsapi_ticket_obj->ticket;
$noncestr = 'qwertyuiop';
$timestamp = time();
$signature = sha1(sprintf('jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url=http://wangbao.bwphp.cn/wx_sample.php'));
?>
<br>########################resourceid<br>
<button onclick="selectimg()">selectimg!!!!</button>
<script src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
<script type="text/javascript">
//配置JS_SDK获取加载JS_SDK文件(jweixin-1.0.0.js)的权限
wx.config({
debug: true,
appId: '<?php echo $appid; ?>',
timestamp: <?php echo $timestamp; ?>,
nonceStr: '<?php echo $noncestr; ?>',
signature: '<?php echo $signature; ?>',
jsApiList: ['uploadImage','downloadImage','previewImage','chooseImage']
});
//定义全局变量
var images = {localIds:[],serverId:[]};
//加载成功后,检测接口权限
wx.ready(function(){
wx.checkJsApi({
jsApiList: ['chooseImage','uploadImage','previewImage','downloadImage'],
success: function(res) {}
});
});
wx.error(function(res){
alert('no');
});
//选择图片
function selectimg(){
wx.chooseImage({
success: function (res) {
images.localIds = res.localIds;
document.getElementById('localimg').src = localIds;
}
});
}
//上传图片,上传的图片在微信的文件服务器存储时间有限,要下载到应用服务器
function uploadimg(){
for(var i = 0 ;i < images.localIds.length ; i++){
wx.uploadImage({
localId: images.localIds[i],
isShowProgressTips: 1,
success: function (res) {
images.serverId.push(res.serverId);
alert(images.serverId);
},
fail: function (res) {
alert("upload fail!"+res.errMsg);
}
});
}
}
</script>