京东开普勒php接口,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 KeplerApi{
private $appKey = 'YourKey'; // 你的Key
private $appScret = 'YourSecret'; // 你的Secret
private $app_token_json = '{}'; // 第一次需要手動授權獲取京東Token然后粘貼到這里
/**
* 獲取開普勒接口數據
* @param string $apiUrl 要獲取的api
* @param string $param_json 該api需要的參數
* @param string $version 版本可選為 2.0
* @param bool $get 是否使用get,默認為post方式
* @return mixed 京東返回的json格式的數據
*/
public function GetKelperApiData($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['param_json'] = json_encode($param_json);
$API['sign_method'] = 'md5';
$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://router.jd.com/api?";
foreach ($API as $k=>$v)
$url .= urlencode($k) . '=' . urlencode($v) . '&'; // 把參數和值url編碼
$url .= 'sign='.$sign; // 接上簽名
$res = self::curl_get($url);
}else{
// 用post方式獲取數據
$url = "https://router.jd.com/api";
$API['sign'] = $sign;
$res = self::curl_post($url,$API);
}
return $res;
}
// 刷新accessToken
private function refreshAccessToken(){
$filePath = dirname(dirname(__FILE__)).'/Config/KelperToken.config'; // Token文本保存路徑
if (file_exists($filePath)){
$handle = fopen($filePath,'r');
$tokenJson = fread($handle,8142);
}else{
// 插入默認的token
fwrite(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); // 解析不了可能是文本出了問題,注意BOM頭
// 判斷
if ($res['code'] == 0){
if ($res['expires_in']*1000 + $res['time'] < self::getMillisecond() - 86400000){ // access_token失效前一天
// 獲取刷新token的url
$refreshUrl = "https://kploauth.jd.com/oauth/token?grant_type=oauth_refresh_token";//&app_key=yourappkey&app_secret=yourappsecret&refresh_token=xxxxxxxx
$refreshUrl .= '&app_key='.$this->appKey;
$refreshUrl .= '&app_secret='.$this->appScret;
$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);
}
}
宙斯接口也是大同小異,無非是換了個域名和授權方式/**
* Class ZeusApi 宙斯接口調用類
*/
class ZeusApi
{
private $appKey = 'YourKey'; // 你的Key
private $appScret = 'YourSecret'; // 你的Secret
private $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;
}
// 刷新accessToken
private function refreshAccessToken(){
$filePath = dirname(dirname(__FILE__)).'/Config/ZeusToken.config'; // Token文本保存路徑
if (file_exists($filePath)){
$handle = fopen($filePath,'r');
$tokenJson = fread($handle,8142);
}else{
// 插入默認的token
fwrite(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接口,PHP调用京东联盟开普勒、宙斯API模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3. 离散小波变换的示例应用
- 下一篇: 【私有git】使用docker搭建git