首頁 > 後端開發 > php教程 > 微信小店呼叫api程式碼範例

微信小店呼叫api程式碼範例

WBOY
發布: 2016-07-29 09:05:30
原創
1028 人瀏覽過

剛開始呼叫微信小店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);             $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"></span><span>"</span><span>;
             print_r( json_decode($ResData) );
        }


        </span><span>//</span><span>获得所有分组</span>        function getAllCategory($state = <span>0</span><span>,$token)
        {
                $url </span>= <span>"</span><span>https://api.weixin.qq.com/merchant/group/getall?access_token=</span><span>"</span><span>.$token;
                $ResData </span>=  cUrlRequest($url,<span>'</span><span>{"status": </span><span>'</span>.$state.<span>'</span><span>}</span><span>'</span><span>);
                echo </span><span>"</span><span><pre class="brush:php;toolbar:false"></span><span>"</span><span>;
                print_r( json_decode($ResData) );
        }

        </span><span>//</span><span>根据分组id来获得商品信息</span>        function getProductByGroudid($state = <span>0</span><span>,$token)
        {
            $url </span>= <span>"</span><span>https://api.weixin.qq.com/merchant/group/getbyid?access_token=</span><span>"</span><span>.$token;
            $ResData </span>=  cUrlRequest($url,<span>'</span><span>{"group_id": </span><span>'</span>.$state.<span>'</span><span>}</span><span>'</span><span>);
            echo </span><span>"</span><span><pre class="brush:php;toolbar:false"></span><span>"</span><span>;
             print_r( json_decode($ResData) );
        }

        </span><span>//</span><span>获取accesstoken</span><span>        function getAccessToken()
        {
                $appid </span>= <span>"</span><span>your appid</span><span>"</span><span>;
                $appsecret </span>= <span>"</span><span>your appsecret</span><span>"</span><span>;
                $url </span>= <span>"</span><span>https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=</span><span>"</span>.$appid.<span>"</span><span>&secret=</span><span>"</span><span>.$appsecret;
                $result </span>=<span> cUrlRequest($url);
                $jsoninfo </span>= json_decode($result, <span>true</span><span>);
                $access_token </span>= $jsoninfo[<span>"</span><span>access_token</span><span>"</span><span>];
                </span><span>return</span><span> $access_token;
        }

        </span><span>//</span><span>根据product_id来获取单个商品</span>        function getOneProductByid($state = <span>0</span><span>,$token)
        {
            $url</span>=<span>"</span><span>https://api.weixin.qq.com/merchant/get?access_token=</span><span>"</span><span>.$token;
            $ResData </span>=  cUrlRequest($url,<span>'</span><span>{"product_id": "</span><span>'</span>.$state.<span>'</span><span>""}</span><span>'</span><span>);
            echo </span><span>"</span><span><pre class="brush:php;toolbar:false"></span><span>"</span><span>;
             print_r( json_decode($ResData) );
        }
        
        </span>        $AccessToken=<span>getAccessToken();
       </span><span>  //</span><span>gStateProduct(1,$AccessToken);
        
        </span><span>//</span><span>getAllCategory(1,$AccessToken);

        </span><span>//</span><span>getProductByGroudid(207061749,$AccessToken);</span><span>        getOneProductByid(</span><span>"</span><span>pf82VwN45zr8eOlur5mAiSTjg8WU</span><span>"</span>,$AccessToken);
登入後複製

以上就介紹了微信小店呼叫api程式碼範例,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板