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


Posted in PHP onNovember 12, 2016

本文实例讲述了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 = '0'){
      $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 '<pre>'; 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['DOCUMENT_ROOT']."/jSon_file/access_token_".$this->ID.".json";
      //print_r($Path);
      if(!file_exists($Path)){
        touch($Path);
        chmod($Path,0777);
      }
      $data['expire_time'] = time() + 7000;
      $data['access_token'] = $accesstoken;
      $fp = fopen($Path, "w");
      fwrite($fp, json_encode($data));
      fclose($fp);
    }
    //读取ACCESSTOKEN
    public function gAccessToken(){
      $Path = $_SERVER['DOCUMENT_ROOT']."/jSon_file/access_token_".$this->ID.".json";
      if(!file_exists($Path)){
        $data['expire_time'] = 0;
        $data['access_token'] = '';
      }
      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,'{"status": '.$state.'}');
       //echo "<pre>";
       print_r( json_decode($ResData) );
    }
    //设置微小店物流支持列表
    public function sLogisticsList(){
      $this->Logistics['Fsearch_code'] = "邮政EMS";
      $this->Logistics['002shentong'] = "申通快递";
      $this->Logistics['066zhongtong'] = "中通速递";
      $this->Logistics['056yuantong'] = "圆通速递";
      $this->Logistics['042tiantian'] = "天天快递";
      $this->Logistics['003shunfeng'] = "顺丰速运";
      $this->Logistics['059Yunda'] = "韵达快运";
      $this->Logistics['064zhaijisong'] = "宅急送";
      $this->Logistics['020huitong'] = "汇通快运";
      $this->Logistics['zj001yixun'] = "易迅快递";
    }
    //获取订单详情
    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,'{"order_id": "'.$order.'"}');
      //$url = "https://api.weixin.qq.com/merchant/order/getbyfilter?access_token=".$this->AccessToken;
      //$ResData = $this->cUrlRequest($url,'{"status": 2}');
      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('begintime' => $firstday,'endtime' => 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" => '0')){
      $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" => '0');
        $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,'{"order_id": "'.$order.'"}');
      print_r( json_decode($ResData) );
    }
}

2. 页面执行代码

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

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php访问查询mysql数据的三种方法
Oct 09 PHP
JS实现php的伪分页
May 25 PHP
PHP 网页过期时间的控制代码
Jun 29 PHP
PHP学习散记_编码(json_encode 中文不显示)
Nov 10 PHP
PHP最常用的2种设计模式工厂模式和单例模式介绍
Aug 14 PHP
ThinkPHP权限认证Auth实例详解
Jul 22 PHP
php实现图片等比例缩放代码
Jul 23 PHP
PHP+Apache+Mysql环境搭建教程
Aug 01 PHP
phalcon model在插入或更新时会自动验证非空字段的解决办法
Dec 29 PHP
Yii1.1框架实现PHP极光推送消息通知功能
Sep 06 PHP
laravel 修改记住我功能的cookie保存时间的方法
Oct 14 PHP
thinkphp框架类库扩展操作示例
Nov 26 PHP
PHP Mysqli 常用代码集合
Nov 12 #PHP
PHP版微信小店接口开发实例
Nov 12 #PHP
PHP错误和异常处理功能模块示例
Nov 12 #PHP
php版微信小店调用api示例代码
Nov 12 #PHP
php实用代码片段整理
Nov 12 #PHP
php中strlen和mb_strlen用法实例分析
Nov 12 #PHP
Yii2单元测试用法示例
Nov 12 #PHP
You might like
多重?l件?合查?(二)
2006/10/09 PHP
javascript 跳转代码集合
2009/12/03 Javascript
Jquery 高亮显示文本中重要的关键字
2009/12/24 Javascript
JS获取当前日期和时间的简单实例
2013/11/19 Javascript
JavaScript开发人员的10个关键习惯小结
2014/12/05 Javascript
JQuery遍历DOM节点的方法
2015/06/11 Javascript
jQuery ajax时间差导致的变量赋值问题分析
2016/01/22 Javascript
酷炫jQuery全屏3D焦点图动画效果
2016/03/22 Javascript
js控住DOM实现发布微博效果
2016/08/30 Javascript
基于vue的换肤功能的示例代码
2017/10/10 Javascript
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2017/11/27 Javascript
如何开发出更好的JavaScript模块
2017/12/22 Javascript
JS代码实现电脑配置检测功能
2018/03/21 Javascript
vue.js的简单自动求和计算实例
2019/11/08 Javascript
浅谈python字符串方法的简单使用
2016/07/18 Python
对python 各种删除文件失败的处理方式分享
2018/04/24 Python
查看python安装路径及pip安装的包列表及路径
2019/04/03 Python
对python3 sort sorted 函数的应用详解
2019/06/27 Python
Python3.7.0 Shell添加清屏快捷键的实现示例
2020/03/23 Python
Python faker生成器生成虚拟数据代码实例
2020/07/20 Python
使用PyCharm安装pytest及requests的问题
2020/07/31 Python
python的flask框架难学吗
2020/07/31 Python
Pytest如何使用skip跳过执行测试
2020/08/13 Python
详解python 支持向量机(SVM)算法
2020/09/18 Python
纯CSS3制作漂亮带动画效果的主机价格表
2015/04/25 HTML / CSS
CSS3常用的几种颜色渐变模式总结
2016/11/18 HTML / CSS
html5桌面通知(Web Notifications)实例解析
2014/07/07 HTML / CSS
资生堂美国官网:Shiseido美国
2016/09/02 全球购物
英国女鞋购物网站:Moda in Pelle
2019/02/18 全球购物
信息技术专业个人自我评价
2013/12/11 职场文书
琅琊山导游词
2015/02/05 职场文书
大学生个人学习总结
2015/02/15 职场文书
工程质量保证书
2015/05/09 职场文书
考研经验交流会策划书
2015/11/02 职场文书
合作意向书范本
2019/04/17 职场文书
python 制作一个gui界面的翻译工具
2021/05/14 Python