本文實(shí)例為大家分享了php獲取微信共享收貨地址的具體代碼,供大家參考,具體內(nèi)容如下
必須是微信授權(quán)目錄 我直接放到了根目錄 其他目錄無(wú)效
/* * 在執(zhí)行此操作之前 首先判斷您是否開通了 微信支付功能 審核通過(guò)后均可使用一下代碼 * 1、設(shè)置微信公眾平臺(tái)網(wǎng)頁(yè)授權(quán) 域名 www.abc.com * 2、設(shè)置下面的 “ 微信參數(shù) ” * 3、把 當(dāng)前文件 index.php 放入根目錄 * 4、用微信訪問(wèn)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ù)請(qǐng)求類型設(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;?>", //公眾號(hào)名稱,由商戶傳入 "timeStamp" : "?php echo $timest?>", //時(shí)間戳 這里隨意使用了一個(gè)值 "nonceStr" : "?php echo $nonceStr;?>", //隨機(jī)串 "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("獲取地址失敗,請(qǐng)重新點(diǎn)擊"); } }); } /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>點(diǎn)擊設(shè)置收貨地址/strong>/a>/div> /section> /body> /html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:海南 佛山 迪慶 樂(lè)山 定西 南寧 六安 十堰
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php獲取微信共享收貨地址的方法》,本文關(guān)鍵詞 php,獲取,微信,共享,收貨,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。