Php store API secondary development and use example, api Secondary Development
This article describes the second API development and usage of the php version. We will share this with you for your reference. The details are as follows:
1. weixiaodian. php page:
<? 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_S SL_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, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output;} // obtain the ACCESSTOKEN public function sAcessToken () {$ this-> HandleAT = $ this-> gAccessToken (); if ($ this-> HandleAT-> expire_time <time () {$ appid = $ this-> AppID; $ appsecret = $ this-> AppSecret; $ ur L = "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-> paccesen en ($ access_token); return $ access_token;} else {return $ this-> HandleAT-> access_token ;}} // Save the ACCESSTOKEN public function pAccessToken ($ accesstoken) {$ Path = $ _ SER VER ['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);} // read 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 ;} // get all items 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);} // sets public function sLogisticsList () {$ this-> Logistics ['fsearch _ Code'] = "Postal EMS"; $ this-> Logistics ['002shentong '] = "Sto "; $ this-> Logistics ['066zhongtong'] = "zhongtong Express"; $ this-> Logistics ['056yuw.g'] = "yuantong Express"; $ t His-> Logistics ['042tiantian '] = "Daily Express Delivery"; $ this-> Logistics ['003shunfeng'] = "SF Express "; $ this-> Logistics ['059yunda '] = "yunda Express"; $ this-> Logistics ['064zhaijisong'] = "Home Delivery "; $ this-> Logistics ['020huitong '] = "huitong Express"; $ this-> Logistics ['zj001yixun'] = "Yi Xun Express ";} // obtain the order details 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);} // query all orders 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 ));} // set the order shipping 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 ));} // close order 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. code execution on the page
<? Phpinclude_once 'class/weixiaodian. php'; $ wXd = new wXd (); echo "<pre>"; // query all products $ wXd-> gStateProduct (); // obtain the order information $ wXd-> gOrderInfo ('20140901'); // close the order $ wXd-> sOrderClose ('20140901 '); // set the shipping order to $ data ['need _ delivery '] = '1'; $ data ['order _ id'] = '2016 '; $ data ['delivery _ company '] = '059yunda'; $ data ['delivery _ track_no'] = '000000'; $ wXd-> sOrderDelivery ($ data ); // obtain all orders $ wXd-> gOrderAll (); echo "</pre> ";