PHP
PHP调用京东宙斯API
PHP不依赖京东宙斯SDK调用京东宙斯API
节前完成了一个需求,需要调用京东宙斯API,由于不想使用京东宙斯SDK(包过大),其实难点就在于签名封装部分,最后成功实现并调用京东宙斯API,在这里跟大家分享我所写的代码。
<?php
/**
* Created by PhpStorm.
* User: HZX
* Date: 2021/9/15
* Time: 10:34
*/
#设置时区,避免请求签名时间过期
date_default_timezone_set('PRC');
/**
* 京东快递API
* Class JdApi
*/
class JdApi
{
#初始化配置
static $JD_APP_KEY = ''; //APP_KEY【默认】
static $JD_APP_SECRET = ''; //APP_SECRET【默认】
static $JD_APP_TOKEN = ''; //APP_TOKEN【默认】
static $JD_CUSTOMER_CODE = ''; //商家编码【默认】
static $JD_API_URL = 'https://api.jd.com/routerjson?'; //API_URL
/**
* 京东快递API-下单【聚合获取订单号-是否京配-下单】
* @param array $params
* @return array
*/
public static function JdApiCreateOrder($params = array())
{
#获取商家编码
$customerCode = self::$JD_CUSTOMER_CODE;
#拼接其他固定参数
$params['salePlat'] = '0030001'; //销售平台(0010001京东商城;0010002天猫、淘宝订单;0030001其他平台)
$params['customerCode'] = $customerCode; //商家编码
$params['packageCount'] = 1; //包裹数(大于0,小于1000)
$params['weight'] = 0.4; //订单总重量(单位:kg,保留小数点后两位)
$params['vloumn'] = 1; //体积(单位:cm3,保留小数点后两位)
$params['promiseTimeType'] = 24; //产品类型(1:特惠送 2:特快送 4:城际闪送 7:微小件 8: 生鲜专送 16:生鲜特快 17、生鲜特惠 20:函数达 21:特惠包裹 24:特惠小件 26:冷链专送)
#调用京东API
$data = self::JdApiGetData('jingdong.ldop.waybill.receive', $params, '2.0', false);
#判断是否调用成功
if (!isset($data['jingdong_ldop_waybill_receive_responce']['code']) && $data['jingdong_ldop_waybill_receive_responce']['code'] !== '0') {
return array(
'code' => 502,
'msg' => isset($data['errorMessage']) ? $data['errorMessage'] : '京东API调用失败',
'data' => $data
);
}
#判断是否是不是超区了-直接进异常
if (isset($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultCode']) && $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultCode'] == 110) {
return array(
'code' => 404,
'msg' => isset($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultMessage']) ? $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultMessage'] : '超区!',
'data' => array()
);
}
#过滤第二层
if (!isset($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['deliveryId']) || empty($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['deliveryId'])) {
return array(
'code' => 404,
'msg' => '京东快递单号获取失败',
'data' => array()
);
}
#判断是否是否存在面单信息【下单成功,没有面单信息,需要重新】
if (!isset($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['preSortResult']) || count($data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['preSortResult']) < 0) {
return array(
'code' => 302,
'msg' => $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultMessage'] ? $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['resultMessage'] : '',
'data' => array(
'deliveryId' => $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['deliveryId'], //运单号
)
);
}
#提取需要的数据
$deliveryId = $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['deliveryId']; //运单号
$preSortResult = $data['jingdong_ldop_waybill_receive_responce']['receiveorderinfo_result']['preSortResult']; //面单打印信息
#返回结果
return array(
'code' => 200,
'msg' => '请求成功',
'data' => array(
'deliveryId' => $deliveryId, //运单号
'preSortResult' => $preSortResult, //面单打印信息
'params' => $params, //请求API参数
)
);
}
/**
* 京东API-取消订单
* @param string $waybillCode
* @return array
*/
public static function JdApiCancelOrder($waybillCode = '')
{
#获取商家编码
$customerCode = self::$JD_CUSTOMER_CODE;
#组合请求参数
$params = array(
'waybillCode' => $waybillCode, //运单号
'customerCode' => $customerCode, //商家编码
'source' => 'JOS', //来源
'cancelReason' => '客户信息有误', //取消原因
'operatorName' => 'API', //操作人
);
#调用京东API
$data = self::JdApiGetData('jingdong.ldop.delivery.provider.cancelWayBill', $params, '2.0', false);
#判断是否调用成功
if (!isset($data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['code']) || $data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['code'] !== '0') {
return array(
'code' => 502,
'msg' => isset($data['errorMessage']) ? $data['errorMessage'] : '京东API调用失败',
'data' => $data
);
}
#过滤第二层
if (!isset($data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['responseDTO']['statusCode']) || $data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['responseDTO']['statusCode'] !== 0) {
return array(
'code' => 404,
'msg' => isset($data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['responseDTO']['statusMessage']) ? $data['jingdong_ldop_delivery_provider_cancelWayBill_responce']['responseDTO']['statusMessage'] : '',
'data' => array()
);
}
#返回结果
return array(
'code' => 200,
'msg' => '取消成功',
'data' => array()
);
}
/**
* 京东快递API-拦截下单
* @param string $deliveryId
* @return array
*/
public static function JdApiInterceptOrder($deliveryId = '')
{
#获取商家编码
$customerCode = self::$JD_CUSTOMER_CODE;
#组合请求参数
$params = array(
'vendorCode' => $customerCode, //商家编码
'deliveryId' => $deliveryId, //取件单号
'interceptReason' => '客户信息有误', //拦截原因
'cancelTime' => date('Y-m-d H:i:s'), //发起取消时间
);
#调用京东API
$data = self::JdApiGetData('jingdong.ldop.receive.order.intercept', $params, '2.0', false);
#判断是否调用成功
if (!isset($data['jingdong_ldop_receive_order_intercept_responce']['code']) || $data['jingdong_ldop_receive_order_intercept_responce']['code'] !== '0') {
return array(
'code' => 502,
'msg' => isset($data['errorMessage']) ? $data['errorMessage'] : '京东API调用失败',
'data' => array()
);
}
#过滤第二层
if (!isset($data['jingdong_ldop_receive_order_intercept_responce']['resultInfo']['stateCode']) && $data['jingdong_ldop_receive_order_intercept_responce']['resultInfo']['stateCode'] !== 100) {
return array(
'code' => 404,
'msg' => '取消揽件失败',
'data' => array()
);
}
#返回结果
return array(
'code' => 200,
'msg' => '拦截成功',
'data' => array()
);
}
/**
* 京东快递API-接口请求
* @param string $method
* @param array $paramList
* @param string $version
* @param bool $get
* @return mixed
*/
public static function JdApiGetData($method = '', $paramList = array(), $version = '2.0', $get = false)
{
#获取密钥信息
$jdAppKey = self::$JD_APP_KEY;
$jdAppSecret = self::$JD_APP_SECRET;
$jdAppToken =self::$JD_APP_TOKEN;
$jdApiUrl = self::$JD_API_URL;
#组合sign
$API['access_token'] = $jdAppToken;
$API['app_key'] = $jdAppKey;
$API['method'] = $method;
$API['360buy_param_json'] = json_encode($paramList);
$API['timestamp'] = date("Y-m-d H:i:s");
$API['v'] = $version;
#排序
ksort($API);
#初始化字符串
$str = '';
#循环拼接加密串
foreach ($API as $k => $v) $str .= $k . $v;
#生成签名-MD5加密转大写
$sign = strtoupper(md5($jdAppSecret . $str . $jdAppSecret));
#判断请求方式
if ($get) {
#初始化请求链接
$url = $jdApiUrl;
foreach ($API as $k => $v)
$url .= urlencode($k) . '=' . $v . '&';
#链接拼接签名串
$url .= 'sign=' . $sign;
#Get请求
$res = self::JdApiCurlGet($url);
} else {
#初始化请求链接
$url = $jdApiUrl;
#初始化签名
$API['sign'] = $sign;
#POST请求
$res = self::JdApiCurlPost($url, $API);
}
#返回
return json_decode($res, true);
}
/**
* 京东快递API-Get请求
* @param string $url
* @return mixed
*/
public static function JdApiCurlGet($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_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 京东快递API-POST请求
* @param string $url
* @param array $curlPost
* @return mixed
*/
public static function JdApiCurlPost($url = '', $curlPost = array())
{
//self::JdApiLog(json_encode($curlPost),'request.txt');
$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_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$result = curl_exec($ch);
curl_close($ch);
//self::JdApiLog($result, 'result.txt');
return $result;
}
/**
* 京东快递API-日志
* @param string $string
* @param string $fileName
*/
public static function JdApiLog($string = '', $fileName = '')
{
$content = "\r\n" . $string . "\r\n";
$path = __DIR__ . "/{$fileName}";
file_put_contents($path, $content, FILE_APPEND);
}
}以上就是调用京东宙斯API类,里面我写了下单、取消单以及拦截单三个方法,大家可以参考。
0条评论