首页 >后端开发 >php教程 >微信小店调用api代码示例

微信小店调用api代码示例

WBOY
WBOY原创
2016-07-29 09:05:301029浏览

刚开始调用微信小店api的时候,可能大家会遇到问题。系统总是提示system error,归根结底还是发送的参数不正确。

下面给出几个调用例子:

例子写得不全。

<span>php
       function cUrlRequest($url,$data </span>= <span>null</span><span>){
            $curl </span>=<span> curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            </span><span>if</span> (!<span>empty($data)){
                curl_setopt($curl, CURLOPT_POST, </span><span>1</span><span>);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, </span><span>1</span><span>);
            $output </span>=<span> curl_exec($curl);
            curl_close($curl);
            </span><span>return</span><span> $output;
        }

        </span><span>//</span><span>获取所有商品</span>        function gStateProduct($state = <span>0</span><span>,$token){
             </span><span>//</span><span>https://api.weixin.qq.com/merchant/getbystatus?access_token=ACCESS_TOKEN</span><span>//</span><span>{"status": 0} 
            </span><span>//</span><span> $AccessToken = "xxxxxxxx";</span>             $url = <span>"</span><span>https://api.weixin.qq.com/merchant/getbystatus?access_token=</span><span>"</span><span>.$token;
             </span><span>//</span><span>print_r($this->AccessToken);</span>             $ResData =  cUrlRequest($url,<span>'</span><span>{"status": </span><span>'</span>.$state.<span>'</span><span>}</span><span>'</span><span>);   //特别注意这里,这个是json格式的。
             echo </span><span>"</span><span><pre class="brush:php;toolbar:false">
"; print_r( json_decode($ResData) ); } //获得所有分组 function getAllCategory($state = 0,$token) { $url = "https://api.weixin.qq.com/merchant/group/getall?access_token=".$token; $ResData = cUrlRequest($url,'{"status": '.$state.'}'); echo "
"; print_r( json_decode($ResData) ); } //根据分组id来获得商品信息 function getProductByGroudid($state = 0,$token) { $url = "https://api.weixin.qq.com/merchant/group/getbyid?access_token=".$token; $ResData = cUrlRequest($url,'{"group_id": '.$state.'}'); echo "
"; print_r( json_decode($ResData) ); } //获取accesstoken function getAccessToken() { $appid = "your appid"; $appsecret = "your appsecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $result = cUrlRequest($url); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo["access_token"]; return $access_token; } //根据product_id来获取单个商品 function getOneProductByid($state = 0,$token) { $url="https://api.weixin.qq.com/merchant/get?access_token=".$token; $ResData = cUrlRequest($url,'{"product_id": "'.$state.'""}'); echo "
"; print_r( json_decode($ResData) ); } $AccessToken=getAccessToken(); //gStateProduct(1,$AccessToken); //getAllCategory(1,$AccessToken); //getProductByGroudid(207061749,$AccessToken); getOneProductByid("pf82VwN45zr8eOlur5mAiSTjg8WU",$AccessToken);

以上就介绍了微信小店调用api代码示例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn