通过京东接口,实现商品信息的同步,商品的库存同步,订单获取,订单状态同步等功能,通过修改京东的SDK当做thinkphp的第三方类库,方便调用。
第一步:在http://jos.jd.com/申请成为开发者,这时候就有了用户的key和secret
第二步:开通 “京东宙斯-京东开放API服务”,然后去http://i.jcloud.com/ 去新建应用
第三步:新建应用里面要求填写回调地址,你可以填写自己电脑的本地地址,那么这个时候就只有你自己这台电脑可以测试http://localhost/admin.php?m=Admin&c=Jd&a=callback,点击保存,这个时候你的应用就是测试应用了,会分配App Key和App Secret给你
第四部:去https://code.jd.com/from201304_m/jos-php-sdk下载原生的PHP-SDK文件,自己可以去看看源码
改造源码步骤
首先我们必须有oauth认证登陆,我在Org/Util文件夹里面建了一个Jdsdk.class.php用于认证登陆授权<?php <br />
namespace Org\Util;<br>
class Jdsdk{<br>
const CSRF_TOKEN = 1;<br>
<br>
const CSRF_AUTHORIZE = 2;<br>
<br>
public $redirectUri;<br>
<br>
protected $authorizeUrl = 'https://oauth.jd.com/oauth/authorize';<br>
<br>
protected $tokenUrl = 'https://oauth.jd.com/oauth/token';<br>
<br>
protected $gatewayUrl='https://api.jd.com/routerjson';<br>
protected $apiVersion='2.0';<br>
<br>
public function __construct(){<br>
$config=C('JOS_CONFIG');<br>
$this->redirectUri=$config['REDIRECT_URI'];<br>
$this->appkey=$config['APP_KEY'];<br>
$this->secretKey=$config['SECRET_KEY'];<br>
}<br>
<br>
/**<br>
* 授权登录<br>
**/<br>
public function getAuthorizeUrl($redirectUri = null){<br>
$redirectUri || $redirectUri = $this->redirectUri;<br>
$param['response_type'] = 'code';<br>
$param['client_id'] = $this->appkey;<br>
$param['redirect_uri'] = $redirectUri;<br>
$param['state'] = $this->mkCsrf(self::CSRF_AUTHORIZE);<br>
$param['scope'] = 'read';<br>
return $this->authorizeUrl . '?' . http_build_query($param);<br>
}<br>
<br>
/**<br>
* 获取accessToken<br>
***/<br>
public function fetchAccessToken($code, $redirectUri = null){<br>
$redirectUri || $redirectUri = $this->redirectUri;<br>
$param = array(<br>
'grant_type' => 'authorization_code',<br>
'client_id' => $this->appkey,<br>
'client_secret' => $this->secretKey,<br>
'code' => $code,<br>
'redirect_uri' => $redirectUri,<br>
'scope' => 'read',<br>
'state' => $this->mkCsrf(self::CSRF_TOKEN)<br>
);<br>
$json = $this->curl($this->tokenUrl, $param);<br>
$json = iconv('gbk', 'utf-8', $json);<br>
$json = json_decode($json);<br>
if (isset($json->code) && isset($json->error_description)) {<br>
throw new \Exception($json->error_description, intval($json->code));<br>
}<br>
return $json;<br>
}<br>
<br>
//获取商品列表信息<br>
//public function <br>
<br>
//执行请求<br>
public function execute(JosRequest $request, $session = null){<br>
$result = new \stdClass();<br>
if ($this->checkRequest) {<br>
try {<br>
$request->check();<br>
} catch (Exception $e) {<br>
$result->code = $e->getCode();<br>
$result->zh_desc = "api请求参数验证失败";<br>
$result->en_desc = $e->getMessage();<br>
return $result;<br>
}<br>
}<br>
// 组装系统参数<br>
$sysParams['app_key'] = $this->appkey;<br>
$sysParams['v'] = $this->apiVersion;<br>
$sysParams['method'] = $request->getApiMethod();<br>
if ($session !== null) {<br>
$sysParams['access_token'] = $_SESSION['token']->access_token;<br>
}<br>
$sysParams['timestamp'] = date('Y-m-d H:i:s');<br>
<br>
// 获取业务参数<br>
$apiParams['360buy_param_json'] = $request->getAppJsonParams();<br>
// 签名<br>
$sysParams['sign'] = $this->generateSign(array_merge($sysParams, $apiParams));<br>
<br>
$requestUrl = $this->gatewayUrl . '?' . http_build_query($sysParams);<br>
//echo $requestUrl;die;<br>
// 发送http请求<br>
try {<br>
$resp = $this->curl($requestUrl, $apiParams);<br>
} catch (Exception $e) {<br>
$result->code = $e->getCode();<br>
$result->zh_desc = "curl发送http请求失败";<br>
$result->en_desc = $e->getMessage();<br>
return $result;<br>
}<br>
// 解析返回结果<br>
$respWellFormed = false;<br>
$respObject = self::jsonDecode($resp);<br>
<br>
if (null !== $respObject) {<br>
$respWellFormed = true;<br>
foreach ($respObject as $propKey => $propValue) {<br>
$respObject = $propValue;<br>
}<br>
}<br>
if (false === $respWellFormed) {<br>
$result->code = 1;<br>
$result->zh_desc = "api返回数据错误或程序无法解析返回参数";<br>
$result->en_desc = "HTTP_RESPONSE_NOT_WELL_FORMED";<br>
$result->resp = $resp;<br>
return $result;<br>
}<br>
return $respObject;<br>
}<br>
<br>
private static function jsonDecode($str)<br>
{<br>
// 特殊字符处理<br>
$str = str_replace([chr(0),chr(4),chr(15),chr(16),chr(19),chr(31),'\\v'], '\\t', $str);<br>
if (defined('JSON_BIGINT_AS_STRING')) {<br>
return json_decode($str, false, 512, JSON_BIGINT_AS_STRING);<br>
} else {<br>
return PhplutilsJSON::decode($str);<br>
}<br>
}<br>
<br>
/**<br>
* 签名<br>
*<br>
* @param $params 业务参数 <br>
* @return void<br>
*/<br>
private function generateSign($params){<br>
if ($params != null) {<br>
ksort($params);<br>
$stringToBeSigned = $this->secretKey;<br>
foreach ($params as $k => $v) {<br>
$stringToBeSigned .= "$k$v";<br>
}<br>
unset($k, $v);<br>
$stringToBeSigned .= $this->secretKey;<br>
} else {<br>
$stringToBeSigned = $this->secretKey;<br>
$stringToBeSigned .= $this->secretKey;<br>
}<br>
return strtoupper(md5($stringToBeSigned));<br>
}<br>
<br>
/**<br>
* 重新生成csrf<br>
*<br>
* @param unknown $key <br>
* @return string<br>
*/<br>
public function mkCsrf($key){<br>
// TODO<br>
// $v = uniqid('', true);<br>
// $_SESSION['jos_' . $key] = $v;<br>
return '';<br>
}<br>
<br>
/**<br>
* 验证csrf正确性,单次验证<br>
*<br>
* @param unknown $key <br>
* @param unknown $value <br>
* @return boolean<br>
*/<br>
public function checkCsrf($key, $value){<br>
// $v = $_SESSION['jos_' . $key];<br>
// $r = $v ? $v === $value : false;<br>
// unset($_SESSION['jos_' . $key]);<br>
// TODO<br>
return true;<br>
}<br>
<br>
//curl请求<br>
public function curl($url, $postFields = null){<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_FAILONERROR, false);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>
// https 请求<br>
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);<br>
}<br>
<br>
if (is_array($postFields) && 0
curl_setopt($ch, CURLOPT_POST, true);<br>
$postMultipart = false;<br>
foreach ($postFields as $k => $v) {<br>
if ('@' == substr($v, 0, 1)) {<br>
$postMultipart = true;<br>
break;<br>
}<br>
}<br>
unset($k, $v);<br>
if ($postMultipart) {<br>
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);<br>
} else {<br>
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));<br>
}<br>
}<br>
$reponse = curl_exec($ch);<br>
<br>
if (curl_errno($ch)) {<br>
throw new \Exception(curl_error($ch), 0);<br>
} else {<br>
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);<br>
if (200 !== $httpStatusCode) {<br>
throw new \Exception($reponse, $httpStatusCode);<br>
}<br>
}<br>
curl_close($ch);<br>
return $reponse;<br>
}<br>
}
然后写一个controller类<?php <br />
// +----------------------------------------------------------------------<br>
// | Author: 前度天下 <br>
// +----------------------------------------------------------------------<br>
<br>
namespace Admin\Controller;<br>
use Think\Controller;<br>
<br>
class JdController extends Controller {<br>
private $jos;<br>
private $jos_config;<br>
public function __construct() {<br>
$this->jos_config = C('JOS_CONFIG'); //这里的配置是写在配置文件中的<br>
$this->jos = new \Org\Util\Jdsdk(); //这里是jd的sdk改造后的类<br>
parent::__construct();<br>
}<br>
<br>
public function index() {<br>
if (isset($_SESSION['is_jd_success'])) {<br>
if ($_SESSION['is_jd_success'] == 1) {<br>
$status = 1;<br>
} elseif ($_SESSION['is_jd_success'] == 0) {<br>
$status = 0;<br>
}<br>
$this->assign("is_jd_success", $status);<br>
}<br>
//获取商品列表<br>
$this->goods = $this->getCateList();<br>
$this->assign("page", $_SESSION['page_num']);<br>
$this->display();<br>
}<br>
<br>
//获取商品列表<br>
public function getCateList($page = 1) {<br>
$obj = new \Org\Util\Jos\WareInfoByInfoRequest();<br>
$obj->setPage($page);<br>
$obj->setPageSize(18);<br>
$resp = $this->jos->execute($obj, $_SESSION['token']->access_token);<br>
//保存页码<br>
$rzt = $resp->total / 18;<br>
$_SESSION['page_num'] = ceil($rzt);<br>
$_SESSION['current_page'] = $page;<br>
return $resp->ware_infos;<br>
}<br>
<br>
//回调地址<br>
public function callback() {<br>
$code = I("code", "", "trim");<br>
$token = $this->jos->fetchAccessToken($code);<br>
if ($token->code != 0) {<br>
$_SESSION['is_jd_success'] = 0;<br>
$this->error("登录失败", U("Jd/index"));<br>
} else {<br>
$_SESSION['token'] = $token;<br>
$_SESSION['is_jd_success'] = 1;<br>
$this->success("登录成功", U("Jd/index"));<br>
}<br>
}<br>
//授权登录<br>
public function login() {<br>
$url = $this->jos->getAuthorizeUrl();<br>
header("location:" . $url);<br>
}<br>
?>
下面是jd的配置文件写在config.php里面 //京东接口配置<br>
'JOS_CONFIG'=>array(<br>
'APP_KEY'=>'',<br>
'SECRET_KEY'=>'',<br>
'REDIRECT_URI'=>'http://localhost/admin.php?m=Admin&c=Jd&a=callback'<br>
),
放一个登陆按钮在页面中,调用控制器的login方法,这个时候就是输入商家的账号和密码,同意授权就登陆成功了
附件中是我对js的sdk的部分改写,下载解压后将Jos文件夹一起放在Org/Util下面
下图中中文件名后缀带有.class.php是我修改了的文件,其他我没有用到就没有修改,修改方法很简单,下面告诉大家
比如:你想获取商品信息,这个时候先去京东的api文档看是哪个方法http://jos.jd.com/api/index.htm
文档中说用的是 360buy.wares.list.get 批量获取商品信息
然后去jos文件夹对所有文件进行全局搜索360buy.wares.list.get这个关键词,
会找到C:\wamp\www\ThinkPHP\Library\Org\Util\Jos\WareListRequest.php
这个时候改造步骤就开始了
先该文件名称WareListRequest.php换成WareListRequest.class.php
打开文件在头部加上命名空间
namespace Org\Util\Jos;
就可以了,接下来是调用
jd的调用输入参数时分为系统级别输入参数和应用级别输入参数,这里已经把系统级别的参数都绑定好了 所以不用管了,直接输入应用级别的参数
对应的就是类中的两个方法
想批量获取商品信息,在controller控制器中写一个方法
例如
function getGoodsList(){
$obj = new \Org\Util\Jos\WareListRequest();
$obj->setWareIds($id); //商品的id,用逗号分隔,最多不能超过10个
$obj->setFields('ware_id'); //需返回的字段列表。可选值:ware结构体中的所有字段;字段之间用“,”分隔
$resp = $this->jos->execute($obj, $_SESSION['token']->access_token); //这段代码所用的调用都需要加上的 都是一样的
print_r($resp);
}
可能有些地方我表达的不清楚,希望各位指出来,提出建议,希望大家有好的代码分享出来,玩过stackoverflow的都知道,国外的码农乐于分享,共同进步,一起分享
Jos.zip ( 42.28 KB 下载:52 次 )
AD:真正免费,域名+虚机+企业邮箱=0元