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 相关文章推荐
数字转英文
Dec 06 PHP
php 特殊字符处理函数
Sep 05 PHP
mysql 查询指定日期时间内sql语句实现原理与代码
Dec 16 PHP
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
Apr 28 PHP
header导出Excel应用示例
Jan 24 PHP
PHP实现的连贯操作、链式操作实例
Jul 08 PHP
php实现使用正则将文本中的网址转换成链接标签
Dec 03 PHP
php使用cookie实现记住用户名和密码实现代码
Apr 27 PHP
php编写简单的文章发布程序
Jun 18 PHP
[原创]php逐行读取txt文件写入数组的方法
Jul 02 PHP
PHP实现链式操作的三种方法详解
Nov 16 PHP
Laravel中正确地返回HTTP状态码方法示例
Sep 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
多重?l件?合查?(一)
2006/10/09 PHP
PHP array_flip() 删除重复数组元素专用函数
2010/05/16 PHP
dojo 之基础篇
2007/03/24 Javascript
百度 popup.js 完美修正版非常的不错 脚本之家推荐
2009/04/17 Javascript
js 金额文本框实现代码
2012/02/14 Javascript
JS应用正则表达式转换大小写示例
2014/09/18 Javascript
js控制鼠标事件移动及移出效果显示
2014/10/19 Javascript
javascript定义变量时带var与不带var的区别分析
2015/01/12 Javascript
jQuery+json实现的简易Ajax调用实例
2015/12/14 Javascript
window.onerror()的用法与实例分析
2016/01/27 Javascript
AngularJS中$interval的用法详解
2016/02/02 Javascript
Javascript动画效果(1)
2016/10/11 Javascript
ES6常用小技巧总结【去重、交换、合并、反转、迭代、计算等】
2019/12/21 Javascript
vue+openlayers绘制省市边界线
2020/12/24 Vue.js
Python中的面向对象编程详解(上)
2015/04/13 Python
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
Python实现简单的语音识别系统
2017/12/13 Python
Python zip()函数用法实例分析
2018/03/17 Python
python寻找list中最大值、最小值并返回其所在位置的方法
2018/06/27 Python
python 定时任务去检测服务器端口是否通的实例
2019/01/26 Python
pycharm+PyQt5+python最新开发环境配置(踩坑)
2019/02/11 Python
Python Matplotlib 基于networkx画关系网络图
2019/07/10 Python
window环境pip切换国内源(pip安装异常缓慢的问题)
2019/12/31 Python
Python 实现Image和Ndarray互相转换
2020/02/19 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
详解python3 GUI刷屏器(附源码)
2021/02/18 Python
飞利信loadrunner和软件测试笔试题
2012/09/22 面试题
大专毕业生简历的自我评价
2013/10/20 职场文书
软件工程专业推荐信
2013/10/28 职场文书
大课间活动实施方案
2014/03/06 职场文书
地方课程教学计划
2015/01/19 职场文书
幼儿园开学通知
2015/04/24 职场文书
初中英语教师个人工作总结2015
2015/07/21 职场文书
mysql脏页是什么
2021/07/26 MySQL
如何打开Win11系统注册表编辑器?Win11注册表编辑器打开修复方法
2022/04/05 数码科技
Ruby GDBM操作简介及数据存储原理
2022/04/19 Ruby