SoFunction
Updated on 2025-04-04

PHP version WeChat automatically obtains delivery address API usage example

This article describes the usage of the PHP version of WeChat to automatically obtain the delivery address API. Share it for your reference, as follows:

The WeChat public platform is becoming more and more powerful now. We can connect with the platform through various API interfaces to obtain corresponding data. Let’s take a look at a program that automatically obtains delivery address of WeChat implemented by PHP. The details are as follows.

I won't introduce the instructions about the interface. You can see the following only the handler below.

public function get_address_api() {
  $APPID=C('APPID');
  $SCRETID=C('SCRETID');
  if (!isset($_GET['code'])) {
    $backurl = $this->get_url();
    $url = "/connect/oauth2/authorize?app&redirect_uri=".urlencode($backurl)."&response_type=code&scope=jsapi_address&state=123#wechat_redirect";
    // snsapi_userinfo
    Header("Location: $url");
    exit;
  } else {
    $code = $_GET['code'];
    $url = "/sns/oauth2/access_token?app&secret=".$SCRETID."&code=".$code."&grant_type=authorization_code";
    $re = file_get_contents($url);
    $rearr = json_decode($re,true);
    $backurl = $this->get_url();
    $openid = $rearr['openid'];
    $unionid = $rearr['unionid'];
    $asstoken = $rearr['access_token'];
    S('jsapi_address_token'.$openid,$asstoken,7200);
    $data['appid']=$APPID;
    $data['url']=$backurl;
    $data['timestamp']=time();
    $data['timestamp']= (string)($data['timestamp']);
    $data['noncestr']=$this->getRandStr(10);
    $data['accesstoken']=$asstoken;
    foreach ($data as $k => $v) {
      $Parameters[$k] = $v;
    }
    //Signature step 1: Sort parameters in dictionary order    ksort($Parameters);
    $String = $this->formatBizQueryParaMap($Parameters, false);
    $data['addrsign']=SHA1($String);
    $this->assign('data',$data);
  }
  $this->siteDisplay('address_api');
}

For more information about PHP related content, please check out the topic of this site:Summary of PHP WeChat development skills》、《Summary of PHP encoding and transcoding operation techniques》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.