PHP version store interface development instance, php store interface instance
This article describes how to develop a PHP store interface. We will share this with you for your reference. The details are as follows:
First, you can go to the next store to develop the API interface because all the store interface data format parameters in the API manual are ready for use. You can use the following code directly.
Here we will download the mini store API documentation
Here we will take the commodity query as an example.
// The first step is to obtain the code for access_token. Here I have saved the token in the table because the token has a limit on private function access_token () {appid = shopappid; // write the appid to your own apps = shop_appsecret during replication; // write the appsecret to your own wxuserdb = M ('wxuser') during replication ′); // you can remove this note !!! Wxuser = wxuserdb −> where (array ('appid '=> appid)-> find (); // obtain access_tokenif (wxuser ['atupdatetime '] = ''| intval (time () −intval (wxuser ['atupdatetime'])> 4000 | $ wxuser ['Access _ token'] = ") {$ url =" https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid. "& secret = ". $ apps; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, expires, FALSE); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ output = curl_exec ($ ch); curl_close ($ ch); $ jsoninfo = json_decode ($ output, true ); $ access_token = $ jsoninfo ["access_token"]; $ wxuser ['Access _ token'] = $ access_token; $ wxuser ['updatetime'] = time (); $ wxuserdb-> where (array ('appid '=> $ appid)-> save ($ wxuser );} else {$ access_token = $ wxuser ['Access _ token'];} return $ access_token ;}
Here I encapsulate the following to get data through the interface
Encapsulated PHP curl () method
private function get_res(url,data){ch=curlinit();curlsetopt(ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt(ch,CURLOPTSSLVERIFYHOST,FALSE);curlsetopt(ch, CURLOPT_URL, url);curlsetopt(ch, CURLOPT_POSTFIELDS, data);curlsetopt(ch, CURLOPT_RETURNTRANSFER, true);output=curlexec(ch);curl_close(ch);jsoninfo = json_decode(output,true);returnjsoninfo;}
/*** Productid get productinfo get Product Information Based on id */private function get_product_info () {wxtoken = this-> access_token (); // get tokenproductid = 'pP3K2s25zDRY50n3nLCKqZvPzINM ′; // item idurl = "https://api.weixin.qq.com/merchant/get? Access_token = ". wxtoken; // The API address data = '{"product_id ":"'. product_id. '"}'; echothis-> get_res (url, data); // exit using the previously encapsulated PHP curl () method ;}
You only need to change the appid and secret.
For other interfaces, you only need to replace the interface address and transmitted data.