PHP调用京东联盟开普勒、宙斯API模板
生活随笔
收集整理的這篇文章主要介紹了
PHP调用京东联盟开普勒、宙斯API模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
京東開普勒的 Appkey 和 AppSecret 在這里可以看到(需要先創建應用):http://kepler.jd.com/console/app/app_list.action
授權介紹在這里:http://kepler.jd.com/console/docCenterCatalog/docContent?channelId=17
宙斯接口也是大同小異,無非是換了個域名和授權方式
/*** Class ZeusApi 宙斯接口調用類*/ class ZeusApi {private $appKey = 'YourKey'; // 你的Keyprivate $appScret = 'YourSecret'; // 你的Secretprivate $app_token_json = '{}'; // 第一次需要手動授權獲取京東Token然后粘貼到這里/*** 獲取宙斯接口數據* @param string $apiUrl 要獲取的api* @param string $param_json 該api需要的參數,使用json格式,默認為 {}* @param string $version 版本可選為 2.0* @param bool $get 是否使用get,默認為post方式* @return mixed 京東返回的json格式的數據*/public function GetZeusApiData($apiUrl='',$param_json = array(),$version='1.0',$get=false){$API['access_token'] = $this->refreshAccessToken(); // 生成的access_token,30天一換$API['app_key'] = $this->appKey;$API['method'] = $apiUrl;$API['360buy_param_json'] = json_encode($param_json);$API['timestamp'] = date('Y-m-d H:i:s',time());$API['v'] = $version;ksort($API); // 排序$str = ''; // 拼接的字符串foreach ($API as $k=>$v) $str.=$k.$v;$sign = strtoupper(md5($this->appScret.$str.$this->appScret)); // 生成簽名 MD5加密轉大寫if ($get){// 用get方式拼接URL$url = "https://api.jd.com/routerjson?";foreach ($API as $k=>$v)$url .= urlencode($k) . '=' . $v . '&'; // 把參數和值url編碼$url .= 'sign='.$sign;$res = self::curl_get($url);}else{// 用post方式獲取數據$url = "https://api.jd.com/routerjson?";$API['sign'] = $sign;$res = self::curl_post($url,$API);}return $res;}// 刷新accessTokenprivate function refreshAccessToken(){$filePath = dirname(dirname(__FILE__)).'/Config/ZeusToken.config'; // Token文本保存路徑if (file_exists($filePath)){$handle = fopen($filePath,'r');$tokenJson = fread($handle,8142);}else{// 插入默認的tokenfwrite(fopen($filePath,'w'),$this->app_token_json);$tokenJson = $this->app_token_json;}if (substr($tokenJson, 0,3) == pack('CCC',0xef,0xbb,0xbf)) {$tokenJson = substr($tokenJson, 3);}$res = json_decode(trim($tokenJson),true); // 解析不了可能是文本出了問題// 判斷if ($res['code'] == 0){if ($res['expires_in']*1000 + $res['time'] < self::getMillisecond() - 86400000){ // access_token失效前一天// 獲取刷新token的url$refreshUrl = "https://oauth.jd.com/oauth/token?";$refreshUrl .= '&client_id='.$this->appKey;$refreshUrl .= '&client_secret='.$this->appScret;$refreshUrl .= '&grant_type=refresh_token';$refreshUrl .= '&refresh_token='.$res['refresh_token'];// 獲取新的token數據$newAccessTokenJson = self::curl_get($refreshUrl);// 寫入文本fwrite(fopen($filePath,'w'),$newAccessTokenJson);// 解析成數組$newAccessTokenArr = json_decode($newAccessTokenJson,true);$accessToken = $newAccessTokenArr['access_token'];}else{$accessToken = $res['access_token'];}return $accessToken;}else{// 如果refresh_token過期,將會返回錯誤碼code:2011;msg:refresh_token過期return $res['msg'];}}// get請求private static function curl_get($url){$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_HEADER, FALSE);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_REFERER, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);$result = curl_exec($ch);curl_close($ch);return $result;}// post請求private static function curl_post($url,$curlPost){$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_REFERER, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);$result = curl_exec($ch);curl_close($ch);return $result;}// 獲取13位時間戳private static function getMillisecond(){list($t1, $t2) = explode(' ', microtime());return sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);} }總結
以上是生活随笔為你收集整理的PHP调用京东联盟开普勒、宙斯API模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是MIT协议?
- 下一篇: 高级架构师,精通JAVA/高并发/微服务