欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-02 08:59:40
本文实例讲述了php版微信小店api二次开发及使用方法。分享给大家供大家参考,具体如下: 1. weixiaodian.php页面:

本文实例讲述了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编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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