本文实例为大家分享了php获取微信共享收货地址的具体代码,供大家参考,具体内容如下
必须是微信授权目录 我直接放到了根目录 其他目录无效
/*
* 在执行此操作之前 首先判断您是否开通了 微信支付功能 审核通过后均可使用一下代码
* 1、设置微信公众平台网页授权 域名 www.abc.com
* 2、设置下面的 “ 微信参数 ”
* 3、把 当前文件 index.php 放入根目录
* 4、用微信访问http://www.abc.com/index.php 就可以了 切记一定是微信哦
* */
<?php
//微信参数
$appid = '*********';
$appsecret = '*****************';
//获取get参数
$code = $_get['code'];
//获取 code
$redirect_uri = 'http://'.$_server['http_host'].$_server['request_uri'];
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".urlencode($redirect_uri)."&response_type=code&scope=jsapi_address&state=cft#wechat_redirect";
if(empty($code)){
header("location: $url");
}
//获取 access_token
$access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$access_token_json = geturl($access_token_url);
$access_token = json_decode($access_token_json,true);
// 定义参数
$timestamp = time();
$noncestr = rand(100000,999999);
$parameters = array();
//===============下面数组 生成sing 使用=====================
$parameters['appid'] = $appid;
$parameters['url'] = $redirect_uri;
$parameters['timestamp'] = "$timestamp";
$parameters['noncestr'] = "$noncestr";
$parameters['accesstoken'] = $access_token['access_token'];
// 生成 sing
$addrsign = gensha1sign($parameters);
function geturl($url){
$opts = array(
curlopt_timeout => 30,
curlopt_returntransfer => 1,
curlopt_ssl_verifypeer => false,
curlopt_ssl_verifyhost => false,
);
/* 根据请求类型设置特定参数 */
$opts[curlopt_url] = $url ;
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return $data;
}
function p($star){
echo '<pre>';
print_r($star);
echo '</pre>';
}
function logtext($content){
$fp=fopen("json.ini","a");
fwrite($fp,"rn".$content);
fclose($fp);
}
//创建签名sha1
function gensha1sign($parameters){
$signpars = '';
ksort($parameters);
foreach($parameters as $k => $v) {
if("" != $v && "sign" != $k) {
if($signpars == '')
$signpars .= $k . "=" . $v;
else
$signpars .= "&". $k . "=" . $v;
}
}
//$signpars = http_build_query($parameters);
$sign = sha1($signpars);
$parameters['sign'] = $sign;
return $sign;
}
?>
<!doctype html>
<html>
<head>
<title>获取共享地址</title>
<meta charset="utf-8" />
<meta id="viewport" name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1; user-scalable=no;" />
</head>
<script language="javascript">
function getaddr(){
weixinjsbridge.invoke('editaddress',{
"appid" : "<?php echo $appid;?>", //公众号名称,由商户传入
"timestamp" : "<?php echo $timestamp;?>", //时间戳 这里随意使用了一个值
"noncestr" : "<?php echo $noncestr;?>", //随机串
"signtype" : "sha1", //微信签名方式:sha1
"addrsign" : "<?php echo $addrsign;?>", //微信签名
"scope" : "jsapi_address"
},function(res){
if(res.err_msg == 'edit_address:ok'){
document.getelementbyid("showaddress").innerhtml="收件人:"+res.username+" 联系电话:"+res.telnumber+" 收货地址:"+res.provicefirststagename+res.addresscitysecondstagename+res.addresscountiesthirdstagename+res.addressdetailinfo+" 邮编:"+res.addresspostalcode;
}
else{
alert("获取地址失败,请重新点击");
}
});
}
</script>
<body>
<style>
section.content{padding:10px 12px;}
section .showaddr{border:1px dashed #c9c9c9;padding:10px 10px 15px;margin-bottom:20px;color:#666666;font-size:12px;text-align:center;}
section .showaddr strong{font-weight:normal;color:#9900ff;font-size:26px;font-family:helvetica;}
</style>
<section class="content">
<div class="showaddr" id="showaddress" ><a id="editaddress" href="javascript:getaddr();" rel="external nofollow" ><strong>点击设置收货地址</strong></a></div>
</section>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!