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环境――Appserv
Dec 13 PHP
快速开发一个PHP扩展图文教程
Dec 12 PHP
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
Aug 09 PHP
基于PHP异步执行的常用方式详解
Jun 03 PHP
PHP中对缓冲区的控制实现代码
Sep 29 PHP
一个简单的php MVC留言本实例代码(必看篇)
Sep 22 PHP
浅谈PHP的反射机制
Dec 15 PHP
php使用curl实现ftp文件下载功能
May 16 PHP
分享5个非常有用的Laravel Blade指令
May 30 PHP
YII2框架中使用RBAC对模块,控制器,方法的权限控制及规则的使用示例
Mar 18 PHP
如何在PHP中使用数组
Jun 09 PHP
laravel ajax curd 搜索登录判断功能的实现
Apr 17 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 中的批处理的实现
2007/06/14 PHP
PHP单例模式详细介绍
2015/07/01 PHP
WordPress迁移时一些常见问题的解决方法整理
2015/11/24 PHP
深入理解PHP中的empty和isset函数
2016/05/26 PHP
PHP钩子实现方法解析
2019/05/21 PHP
php给数组赋值的实例方法
2019/09/26 PHP
aspx中利用js实现确认删除代码
2010/07/22 Javascript
js 函数调用模式小结
2011/12/26 Javascript
Angular用来控制元素的展示与否的原生指令介绍
2015/01/07 Javascript
Jquery注册事件实现方法
2015/05/18 Javascript
VUE axios上传图片到七牛的实例代码
2017/07/28 Javascript
浅谈vue项目如何打包扔向服务器
2018/05/08 Javascript
Nodejs异步回调之异常处理实例分析
2018/06/22 NodeJs
微信小程序class封装http代码实例
2019/08/24 Javascript
js实现小星星游戏
2020/03/23 Javascript
小程序实现录音功能
2020/09/22 Javascript
python获取从命令行输入数字的方法
2015/04/29 Python
常用python编程模板汇总
2016/02/12 Python
浅谈Django学习migrate和makemigrations的差别
2018/01/18 Python
python opencv设置摄像头分辨率以及各个参数的方法
2018/04/02 Python
Python实现多属性排序的方法
2018/12/05 Python
Python使用requests提交HTTP表单的方法
2018/12/26 Python
深入浅析Python中的迭代器
2019/06/04 Python
python-OpenCV 实现将数组转换成灰度图和彩图
2020/01/09 Python
Python unittest工作原理和使用过程解析
2020/02/24 Python
微信浏览器左上角返回按钮拦截功能
2017/11/21 HTML / CSS
印度首选时尚目的地:Reliance Trends
2018/01/17 全球购物
新秀丽官方旗舰店:Samsonite拉杆箱、双肩包、皮具
2018/03/05 全球购物
小学生学习雷锋倡议书
2014/05/15 职场文书
农村文化活动总结
2014/08/28 职场文书
党的群众路线教育实践活动总结材料
2014/10/30 职场文书
实习科室评语
2015/01/04 职场文书
学校施工安全责任书
2015/01/29 职场文书
盗窃罪辩护词范文
2015/05/21 职场文书
go select编译期的优化处理逻辑使用场景分析
2021/06/28 Golang
Win11如何查看显卡型号 Win11查看显卡型号的方法
2022/08/14 数码科技