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 iis5下安装php4.0+mysql之我见
Oct 09 PHP
php 魔术函数使用说明
Feb 21 PHP
CI框架源码阅读,系统常量文件constants.php的配置
Feb 28 PHP
php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
Dec 15 PHP
php实现从上传文件创建缩略图的方法
Apr 02 PHP
php实现图片等比例缩放代码
Jul 23 PHP
PHP实现长文章分页实例代码(附源码)
Feb 03 PHP
thinkPHP3.1验证码的简单实现方法
Apr 22 PHP
[原创]php token使用与验证示例【测试可用】
Aug 30 PHP
Yii框架参数配置文件params用法实例分析
Sep 11 PHP
基于thinkphp6.0的success、error实现方法
Nov 05 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
通过ICQ网关发送手机短信的PHP源程序
2006/10/09 PHP
php设计模式 Builder(建造者模式)
2011/06/26 PHP
对淘宝URL中ID提取的PHP代码
2013/09/01 PHP
destoon实现商铺管理主页设置增加新菜单的方法
2014/06/26 PHP
PHP MPDF中文乱码的解决方式
2015/12/08 PHP
基于Laravel实现的用户动态模块开发
2017/09/21 PHP
PHP children()函数讲解
2019/02/03 PHP
PHP的imageTtfText()函数深入详解
2021/03/03 PHP
在IE中调用javascript打开Excel的代码(downmoon原作)
2007/04/02 Javascript
JS Loading功能的简单实现
2013/11/29 Javascript
jquery 显示*天*时*分*秒实现时间计时器
2014/05/07 Javascript
JS动态创建DOM元素的方法
2015/06/09 Javascript
js实现左侧网页tab滑动门效果代码
2015/09/06 Javascript
JavaScript设计模式经典之工厂模式
2016/02/24 Javascript
DOM 事件的深入浅出(二)
2016/12/05 Javascript
原生js实现可拖动的登录框效果
2017/01/21 Javascript
详解在Angular项目中添加插件ng-bootstrap
2017/07/04 Javascript
JavaScript模块模式实例详解
2017/10/25 Javascript
nodejs如何在package.json中设置多条启动命令
2020/03/16 NodeJs
Vue优化:常见会导致内存泄漏问题及优化详解
2020/08/04 Javascript
vue实践---vue不依赖外部资源实现简单多语操作
2020/09/21 Javascript
详解Django通用视图中的函数包装
2015/07/21 Python
Python实现向服务器请求压缩数据及解压缩数据的方法示例
2017/06/09 Python
Python使用progressbar模块实现的显示进度条功能
2018/05/31 Python
利用pandas将numpy数组导出生成excel的实例
2018/06/14 Python
python 实现分页显示从es中获取的数据方法
2018/12/26 Python
Python使用正则表达式分割字符串的实现方法
2019/07/16 Python
opencv resize图片为正方形尺寸的实现方法
2019/12/26 Python
Python获取对象属性的几种方式小结
2020/03/12 Python
PyCharm License Activation激活码失效问题的解决方法(图文详解)
2020/03/12 Python
django 利用Q对象与F对象进行查询的实现
2020/05/15 Python
巴西最大的珠宝连锁店:Vivara
2019/04/18 全球购物
历史专业个人求职信范文
2013/12/07 职场文书
职工运动会邀请函
2014/02/02 职场文书
关于建议书的格式范文
2014/05/20 职场文书
团组织推优材料
2014/12/29 职场文书