当前位置:首页 > PHP教程 > php高级应用 > 列表

php获取微信共享收货地址的方法

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-04 13:36:16 浏览: 评论:0 

本文实例为大家分享了php获取微信共享收货地址的具体代码,供大家参考,具体内容如下.

必须是微信授权目录 我直接放到了根目录 其他目录无效.

  1. /*  
  2. * 在执行此操作之前 首先判断您是否开通了 微信支付功能 审核通过后均可使用一下代码  
  3. * 1、设置微信公众平台网页授权 域名 www.abc.com  
  4. * 2、设置下面的 “ 微信参数 ”  
  5. * 3、把 当前文件 index.php 放入根目录  
  6. * 4、用微信访问http://www.abc.com/index.php 就可以了 切记一定是微信哦  
  7. * */ 
  8. <?php 
  9. //微信参数 
  10. $appId = '*********'; 
  11. $appSecret = '*****************'; 
  12.   
  13. //获取get参数 
  14. $code = $_GET['code']; 
  15.   
  16. //获取 code 
  17. $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
  18. $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"; 
  19. if(emptyempty($code)){ 
  20.  header("location: $url"); 
  21. } 
  22.   
  23. //获取 access_token 
  24. $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code"; 
  25. $access_token_json = getUrl($access_token_url); 
  26. $access_token = json_decode($access_token_json,true); 
  27.   
  28.   
  29. // 定义参数 
  30. $timestamp = time(); 
  31. $nonceStr = rand(100000,999999); 
  32. $Parameters = array(); 
  33. //===============下面数组 生成SING 使用===================== 
  34. $Parameters['appid'] = $appId; 
  35. $Parameters['url'] = $redirect_uri; 
  36. $Parameters['timestamp'] = "$timestamp"; 
  37. $Parameters['noncestr'] = "$nonceStr"; 
  38. $Parameters['accesstoken'] = $access_token['access_token']; 
  39. // 生成 SING 
  40. $addrSign = genSha1Sign($Parameters); 
  41.   
  42.   
  43. function getUrl($url){ 
  44.  $opts = array( 
  45.   CURLOPT_TIMEOUT  => 30, 
  46.   CURLOPT_RETURNTRANSFER => 1, 
  47.   CURLOPT_SSL_VERIFYPEER => false, 
  48.   CURLOPT_SSL_VERIFYHOST => false, 
  49.  ); 
  50.  /* 根据请求类型设置特定参数 */ 
  51.  $opts[CURLOPT_URL] = $url ; 
  52.  $ch = curl_init(); 
  53.  curl_setopt_array($ch, $opts); 
  54.  $data = curl_exec($ch); 
  55.  $error = curl_error($ch); 
  56.  curl_close($ch); 
  57.  return $data; 
  58. } 
  59. function p($star){ 
  60.  echo '<pre>'; 
  61.  print_r($star); 
  62.  echo '</pre>'; 
  63. } 
  64. function logtext($content){ 
  65.  $fp=fopen("json.ini","a"); 
  66.  fwrite($fp,"\r\n".$content); 
  67.  fclose($fp); 
  68. } 
  69. //创建签名SHA1 
  70. function genSha1Sign($Parameters){ 
  71.  $signPars = ''; 
  72.  ksort($Parameters); 
  73.  foreach($Parameters as $k => $v) { 
  74.   if("" != $v && "sign" != $k) { 
  75.    if($signPars == '') 
  76.     $signPars .= $k . "=" . $v; 
  77.    else 
  78.     $signPars .= "&". $k . "=" . $v; 
  79.   } 
  80.  } 
  81.  //$signPars = http_build_query($Parameters); 
  82.  $sign = SHA1($signPars); 
  83.  $Parameters['sign'] = $sign; 
  84.  return $sign; 
  85. } 
  86. ?> 
  87.  
  88. <!DOCTYPE html> 
  89. <html> 
  90. <head> 
  91.  <title>获取共享地址</title> 
  92.  <meta charset="utf-8" /> 
  93.  <meta id="viewport" name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1; user-scalable=no;" /> 
  94. </head> 
  95. <script language="javascript"> 
  96.  function getaddr(){ 
  97.   WeixinJSBridge.invoke('editAddress',{ 
  98.    "appId" : "<?php echo $appId;?>", //公众号名称,由商户传入 
  99.    "timeStamp" : "<?php echo $timestamp;?>", //时间戳 这里随意使用了一个值 
  100.    "nonceStr" : "<?php echo $nonceStr;?>", //随机串 
  101.    "signType" : "SHA1", //微信签名方式:sha1 
  102.    "addrSign" : "<?php echo $addrSign;?>", //微信签名 
  103.    "scope" : "jsapi_address" 
  104.   },function(res){ 
  105.    if(res.err_msg == 'edit_address:ok'){ 
  106.     document.getElementById("showAddress").innerHTML="收件人:"+res.userName+" 联系电话:"+res.telNumber+" 收货地址:"+res.proviceFirstStageName+res.addressCitySecondStageName+res.addressCountiesThirdStageName+res.addressDetailInfo+" 邮编:"+res.addressPostalCode; 
  107.    } 
  108.    else{ 
  109.     alert("获取地址失败,请重新点击"); 
  110.    } 
  111.   }); 
  112.  } 
  113. </script> 
  114. <body> 
  115. <style> 
  116.  section.content{padding:10px 12px;} 
  117.  section .showaddr{border:1px dashed #C9C9C9;padding:10px 10px 15px;margin-bottom:20px;color:#666666;font-size:12px;text-align:center;} 
  118.  section .showaddr strong{font-weight:normal;color:#9900FF;font-size:26px;font-family:Helvetica;} 
  119. </style> 
  120.   
  121. <section class="content"> 
  122.  <div class="showaddr" id="showAddress" ><a id="editAddress" href="javascript:getaddr();" rel="external nofollow" ><strong>点击设置收货地址</strong></a></div> 
  123. </section> 
  124. </body> 
  125. </html> 

Tags: php微信共享 php共享收货地址

分享到: