這篇文章主要為大家詳細介紹了php擷取共用收貨地址的方法,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文執行個體為大家分享了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,"\r\n".$content); fclose($fp);}//建立簽名SHA1function 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"> <p class="showaddr" id="showAddress" ><a id="editAddress" href="javascript:getaddr();" rel="external nofollow" ><strong>點擊設定收貨地址</strong></a></p></section></body></html>