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 相关文章推荐
让你的WINDOWS同时支持MYSQL4,MYSQL4.1,MYSQL5X
Dec 06 PHP
php _autoload自动加载类与机制分析
Feb 10 PHP
PHP 杂谈《重构-改善既有代码的设计》之五 简化函数调用
May 07 PHP
PHP伪静态Rewrite设置之APACHE篇
Jul 30 PHP
PHP实现图片旋转效果实例代码
Oct 01 PHP
又十个超级有用的PHP代码片段
Sep 24 PHP
php获取当前页面完整URL地址
Dec 30 PHP
YII Framework框架教程之使用YIIC快速创建YII应用详解
Mar 15 PHP
thinkPHP实现签到功能的方法
Mar 15 PHP
基于PHP-FPM进程池探秘
Oct 17 PHP
Yii2语言国际化自动配置详解
Aug 22 PHP
ThinkPHP框架整合微信支付之刷卡模式图文详解
Apr 10 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
php开发留言板的CRUD(增,删,改,查)操作
2012/04/19 PHP
浅析PHP程序设计中的MVC编程思想
2014/07/28 PHP
php利用事务处理转账问题
2015/04/22 PHP
浅析Yii2 GridView实现下拉搜索教程
2016/04/22 PHP
php+webSoket实现聊天室示例代码(附源码)
2017/02/17 PHP
在 Laravel 中 “规范” 的开发短信验证码发送功能
2017/10/26 PHP
捕获关闭窗口的脚本
2009/01/10 Javascript
jquery.ui.progressbar 中文文档
2009/11/26 Javascript
BOM与DOM的区别分析
2010/10/26 Javascript
JavaScript中的Function函数
2015/08/27 Javascript
基于jQuery和CSS3制作数字时钟附源码下载(jquery篇)
2015/11/24 Javascript
轻松实现Bootstrap图片轮播
2020/04/20 Javascript
touch.js 拖动、缩放、旋转 (鼠标手势)功能代码
2017/02/04 Javascript
Express与NodeJs创建服务器的两种方法
2017/02/06 NodeJs
Jquery动态列功能完整实例
2019/08/30 jQuery
Vue.js中Line第三方登录api的实现代码
2020/06/29 Javascript
[02:47]3.19DOTA2发布会 国服成长历程回顾
2014/03/25 DOTA
[02:20]2014DOTA2西雅图邀请赛 MVP外卡赛首胜采访
2014/07/09 DOTA
Python开发编码规范
2006/09/08 Python
详解Django的CSRF认证实现
2018/10/09 Python
Python Matplotlib 基于networkx画关系网络图
2019/07/10 Python
Python 使用matplotlib模块模拟掷骰子
2019/08/08 Python
利用Canvas模仿百度贴吧客户端loading小球的方法示例
2017/08/13 HTML / CSS
12个不为大家熟知的HTML5设计小技巧
2016/06/02 HTML / CSS
台湾饭店和机票预订网站:Expedia台湾
2016/08/05 全球购物
英国优质家居用品网上品牌:URBANARA
2018/06/01 全球购物
逻辑链路控制协议
2016/10/01 面试题
大学生党员自我评价范文
2014/04/09 职场文书
广告学专业毕业生自荐信
2014/05/28 职场文书
2014年大学生党员评议表自我评价
2014/09/20 职场文书
2014年社区卫生工作总结
2014/12/18 职场文书
2016春节慰问信范文
2015/03/25 职场文书
论语读书笔记
2015/06/26 职场文书
Vue实现动态查询规则生成组件
2021/05/27 Vue.js
Python实战之OpenCV实现猫脸检测
2021/06/26 Python
磁贴还没死, 微软Win11可修改注册表找回Win10开始菜单
2021/11/21 数码科技