登录  /  注册

php版微信小店API二次开发及使用示例

黄舟
发布: 2017-01-03 09:17:37
原创
1773人浏览过

本文实例讲述了php版微信小店api二次开发及使用方法。分享给大家供大家参考,具体如下:

1. weixiaodian.php页面:

<?php
  class wXd
  {
    public $AppID = "";
    public $AppSecret = "";
    public $OutPut = "";
    public $AccessToken = "";
    public $ID = "";
    public $HandleAT = array();
    public $Logistics = array();
    public function __construct($ID = &#39;0&#39;){
      $this->ID = $ID;
      $this->sLogisticsList();
    }
    public function cUrlRequest($url,$data = null){
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
      if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      }
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $output = curl_exec($curl);
      curl_close($curl);
      return $output;
    }
    //获取ACCESSTOKEN
    public function sAcessToken(){
      $this->HandleAT = $this->gAccessToken();
      if($this->HandleAT->expire_time < time()){
        $appid = $this->AppID;
        $appsecret = $this->AppSecret;
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        $result = https_request($url);
        //echo &#39;<pre class="brush:php;toolbar:false">&#39;; var_dump($result);die;
        $jsoninfo = json_decode($result, true);
        $access_token = $jsoninfo["access_token"];
        $this->pAccessToken($access_token);
        return $access_token;
      }
      else{
        return $this->HandleAT->access_token;
      }
    }
    //保存ACCESSTOKEN
    public function pAccessToken($accesstoken){
      $Path = $_SERVER[&#39;DOCUMENT_ROOT&#39;]."/jSon_file/access_token_".$this->ID.".json";
      //print_r($Path);
      if(!file_exists($Path)){
        touch($Path);
        chmod($Path,0777);
      }
      $data[&#39;expire_time&#39;] = time() + 7000;
      $data[&#39;access_token&#39;] = $accesstoken;
      $fp = fopen($Path, "w");
      fwrite($fp, json_encode($data));
      fclose($fp);
    }
    //读取ACCESSTOKEN
    public function gAccessToken(){
      $Path = $_SERVER[&#39;DOCUMENT_ROOT&#39;]."/jSon_file/access_token_".$this->ID.".json";
      if(!file_exists($Path)){
        $data[&#39;expire_time&#39;] = 0;
        $data[&#39;access_token&#39;] = &#39;&#39;;
      }
      else{
        $data = json_decode(file_get_contents($Path));
        //print_r($data);
      }
      return $data;
    }
    //获取所有商品
    public function gStateProduct($state = 0){
       //https://api.weixin.qq.com/merchant/getbystatus?access_token=ACCESS_TOKEN
       //{"status": 0}
       $this->AccessToken = $this->sAcessToken();
       $url = "https://api.weixin.qq.com/merchant/getbystatus?access_token=".$this->AccessToken;
       //print_r($this->AccessToken);
       $ResData = $this->cUrlRequest($url,&#39;{"status": &#39;.$state.&#39;}&#39;);
       //echo "<pre class="brush:php;toolbar:false">";
       print_r( json_decode($ResData) );
    }
    //设置微小店物流支持列表
    public function sLogisticsList(){
      $this->Logistics[&#39;Fsearch_code&#39;] = "邮政EMS";
      $this->Logistics[&#39;002shentong&#39;] = "申通快递";
      $this->Logistics[&#39;066zhongtong&#39;] = "中通速递";
      $this->Logistics[&#39;056yuantong&#39;] = "圆通速递";
      $this->Logistics[&#39;042tiantian&#39;] = "天天快递";
      $this->Logistics[&#39;003shunfeng&#39;] = "顺丰速运";
      $this->Logistics[&#39;059Yunda&#39;] = "韵达快运";
      $this->Logistics[&#39;064zhaijisong&#39;] = "宅急送";
      $this->Logistics[&#39;020huitong&#39;] = "汇通快运";
      $this->Logistics[&#39;zj001yixun&#39;] = "易迅快递";
    }
    //获取订单详情
    public function gOrderInfo($order){
      $this->AccessToken = $this->sAcessToken();
      //print_r($this->AccessToken);
      $url = "https://api.weixin.qq.com/merchant/order/getbyid?access_token=".$this->AccessToken;
      $ResData = $this->cUrlRequest($url,&#39;{"order_id": "&#39;.$order.&#39;"}&#39;);
      //$url = "https://api.weixin.qq.com/merchant/order/getbyfilter?access_token=".$this->AccessToken;
      //$ResData = $this->cUrlRequest($url,&#39;{"status": 2}&#39;);
      print_r( json_decode($ResData) );
    }
    //查询全部订单
    public function gOrderAll($data = array()){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/getbyfilter?access_token=".$this->AccessToken;
      if(!empty($data)){
        $data = json_encode($data);
      }
      else{
        $firstday = strtotime(date("Y-m-01",time()));
        $data = array(&#39;begintime&#39; => $firstday,&#39;endtime&#39; => strtotime("$firstday +1 month -1 day"));
        $data = json_encode($data);
      }
      $ResData = $this->cUrlRequest($url,$data);
      print_r( json_decode($ResData) );
    }
    //设置订单发货
    public function sOrderDelivery($data = array("need_delivery" => &#39;0&#39;)){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/setdelivery?access_token=".$this->AccessToken;
      if(!empty($data)){
        $data = json_encode($data);
      }
      else{
        $data = array("need_delivery" => &#39;0&#39;);
        $data = json_encode($data);
      }
      $ResData = $this->cUrlRequest($url,$data);
      print_r( json_decode($ResData) );
    }
    //关闭订单
    public function sOrderClose($order){
      $this->AccessToken = $this->sAcessToken();
      $url = "https://api.weixin.qq.com/merchant/order/close?access_token=".$this->AccessToken;
      $ResData = $this->cUrlRequest($url,&#39;{"order_id": "&#39;.$order.&#39;"}&#39;);
      print_r( json_decode($ResData) );
    }
}
登录后复制

2. 页面执行代码

<?php
include_once &#39;class/weixiaodian.php&#39;;
$wXd = new wXd();
echo "<pre class="brush:php;toolbar:false">";
//查询全部商品
$wXd->gStateProduct();
//获取订单信息
$wXd->gOrderInfo(&#39;12963133879983601645&#39;);
//关闭订单
$wXd->sOrderClose(&#39;12963133879983600740&#39;);
//发货订单设置
$data[&#39;need_delivery&#39;] = &#39;1&#39;;
$data[&#39;order_id&#39;] = &#39;12963133879983600667&#39;;
$data[&#39;delivery_company&#39;] = &#39;059Yunda&#39;;
$data[&#39;delivery_track_no&#39;] = &#39;1000464090326&#39;;
$wXd->sOrderDelivery($data);
//获取所有订单
$wXd->gOrderAll();
echo "
登录后复制
";

以上就是php版微信小店API二次开发及使用示例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号