Home  >  Article  >  php教程  >  微信小店的接口开发实例PHP

微信小店的接口开发实例PHP

WBOY
WBOYOriginal
2016-06-06 19:46:22903browse

首先 大家可以去下一份小店 开发 的 API 接口 因为 下面所有的 微信小店 接口 数据格式 参数 API手册 里面都有现成的 你可以直接拿来用 好了 下面上代码 这里给大家 下载微小店 API文档 就先拿查询商品作为例子 //首先第一步是 获取access_token的代码 我这

首先 大家可以去下一份小店开发的 API接口 因为 下面所有的 微信小店接口 数据格式 参数 API手册 里面都有现成的 你可以直接拿来用 好了 下面上代码
这里给大家 下载微小店 API文档

微信小店的接口开发实例PHP
就先拿查询商品作为例子
//首先第一步是 获取access_token的代码 我这里呢 对token做了存表里的 因为token有限制
private function access_token(){
appid=shopappid;//appidapps=shop_appsecret;//复制的时候 将appsecret写上你自己的
wxuserdb=M(Wxuser);//wxuser=wxuserdb?>where(array(appid=>appid))->find();
//得到access_token
if(wxuser[atupdatetime]==′′||intval(time())?intval(wxuser[‘atupdatetime’])>4000||$wxuser[‘access_token’]==”“){

<code>        $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, CURLOPT_SSL_VERIFYHOST, 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['atupdatetime']=time();
        $wxuserdb->where(array('appid'=>$appid))->save($wxuser);
    }else{
        $access_token = $wxuser['access_token'];
    }
    return $access_token;
}
</code>

//这里我封装了下 是通过接口 获取数据
封装的 PHP curl()方法
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根据id获取商品信息
*/
private function get_product_info(){
wxtoken=this->access_token();//获取到token
productid=pP3K2s25zDRY50n3nLCKqZvPzINM;//idurl = "https://api.weixin.qq.com/merchant/get?access_token=".wxtoken;//iddata='{ "product_id": "'.product_id.’”} ‘;  
        echo
this->get_res(url,data);//通过之前封装的 PHP curl()方法
exit;
}

需要 拿去直接用的 朋友只需要更改appid 和 secret
其他接口 只需要换掉 接口地址 和 传输的 数据

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn