本文實例為大家分享了php獲取微信共享收貨地址的具體代碼,供大家參考,具體內(nèi)容如下
必須是微信授權(quán)目錄 我直接放到了根目錄 其他目錄無效
/*
* 在執(zhí)行此操作之前 首先判斷您是否開通了 微信支付功能 審核通過后均可使用一下代碼
* 1、設(shè)置微信公眾平臺網(wǎng)頁授權(quán) 域名 www.abc.com
* 2、設(shè)置下面的 “ 微信參數(shù) ”
* 3、把 當(dāng)前文件 index.php 放入根目錄
* 4、用微信訪問http://www.abc.com/index.php 就可以了 切記一定是微信哦
* */
?php
//微信參數(shù)
$appId = '*********';
$appSecret = '*****************';
//獲取get參數(shù)
$code = $_GET['code'];
//獲取 code
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appIdredirect_uri=".urlencode($redirect_uri)."response_type=codescope=jsapi_addressstate=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);
// 定義參數(shù)
$timestamp = time();
$nonceStr = rand(100000,999999);
$Parameters = array();
//===============下面數(shù)組 生成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,
);
/* 根據(jù)請求類型設(shè)置特定參數(shù) */
$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,"\r\n".$content);
fclose($fp);
}
//創(chuàng)建簽名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 $timest?>", //時間戳 這里隨意使用了一個值
"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+" 聯(lián)系電話:"+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>點擊設(shè)置收貨地址/strong>/a>/div>
/section>
/body>
/html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- php版微信自動獲取收貨地址api用法示例
- php開發(fā)微信支付獲取用戶地址