本文實例講述了php版微信小店呼叫api的方法。分享給大家供大家參考,具體如下:
剛開始調用微信小店api的時候,可能大家會遇到問題。系統總是提示system error,歸根結底還是發送的參數不正確。
下面給幾個呼叫範例:
範例寫得不全。
<?php function cUrlRequest($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_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, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } //获取所有商品 function gStateProduct($state = 0,$token){ //https://api.weixin.qq.com/merchant/getbystatus?access_token=ACCESS_TOKEN //{"status": 0} // $AccessToken = "xxxxxxxx"; $url = "https://api.weixin.qq.com/merchant/getbystatus?access_token=".$token; //print_r($this->AccessToken); $ResData = cUrlRequest($url,'{"status": '.$state.'}'); //特别注意这里,这个是json格式的。 echo "<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 "<pre class="brush:php;toolbar:false">"; 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 "<pre class="brush:php;toolbar:false">"; 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 "<pre class="brush:php;toolbar:false">"; print_r( json_decode($ResData) ); } $AccessToken=getAccessToken(); //gStateProduct(1,$AccessToken); //getAllCategory(1,$AccessToken); //getProductByGroudid(207061749,$AccessToken); getOneProductByid("pf82VwN45zr8eOlur5mAiSTjg8WU",$AccessToken);
以上就是php版微信小店呼叫api範例程式碼的內容,更多相關內容請關注PHP中文網(www.php.cn)!