背景:项目需要使用京东的物流服务,中间各种交流、签合同过程不做赘述,作为程序员,凭什么总要依靠代码实现能力来判断,鬼知道自己哪天是什么样子,以后不做程序猿,也是一条好汉!可惜,口水吐完还得老老实实来搬砖。
其实,很不喜欢泛泛而谈,以下是实现授权的操作流程,仅做参考。
1.平台配置信息
(1).首先创建应用,然后进行授权12345...在新建的应用下配置回调路径,以方便测试。
(2).建议熟悉京东云宙斯的技术开发文档
(3).在回调函数设置正确的前提下,点击测试按钮,然后进行账户密码的登录授权
(4).此时很有可能报出页面失效等提示,后面提供了简单的测试代码
2.提示:
(1).注意回调Url的唯一性(2).回调Url会返回不同情况下的信息,注意GET或POST的不同。
(3).因为根据应用的状态,access_token的时效性是不同的,有24小时的、有1年的。所以,可以将获得的access_token 以及账号公用信息存储到数据库中,以备后面的使用,等到下次时效到期,重新存储就好。
3.使用JOS所提供的php-sdk
(简单举例:获取京东物流订单信息--此处使用的是京东物流)(1).注意:此处我使用的是ThinkPHP框架,所以在实例化类的时候,需要使用类似如下的代码:
Vendor('Jos.jd.JdClient');
(2).开发文档中有明确指出
——正式环境授权地址:https://oauth.jd.com/oauth/authorize? (需要拼接参数,无法直接访问)
——Https调用入口地址:https://api.jd.com/routerjson
参考代码如下:
$this->server_url = "https://api.jd.com/routerjson";
4.附录代码文件:
<?php namespace M\Controller; use Common\Model\JosModel; use Think\Controller; /* 红酒奖励 控制器 */ class JosController extends Controller { private $app_key;//应用的app_key private $app_secret;//即创建应用时的Appsecret(从JOS控制台->管理应用中获取) private $expires_in;//失效时间(从当前时间算起,单位:秒) private $access_token;//JOS 所回传的access_token值 private $refresh_token;//即授权时获取的刷新令牌 private $time;//授权的时间点(UNIX时间戳,单位:毫秒) private $jd_client ; private $server_url; public function __construct() { Vendor('Jos.jd.JdClient'); $model = new JosModel(); $res = $model->getData(); $info = $res[0]; $this->app_key = $info['app_key']; $this->app_secret = $info['app_secret']; $this->expires_in = $info['expires_in']; $this->access_token = $info['access_token']; $this->refresh_token = $info['refresh_token']; $this->time = $info['time']; $this->jd_client = new \JdClient(); $this->server_url = "https://api.jd.com/routerjson"; } public function oauth(){ $code = $_GET['code']; $appKey = 'DExxxxxxxxxxxxxxxxxxxxxx83'; $appSecret = '40xxxxxxxxxxxxxxxxxxxxxxxxx31'; $url = "http://www.xxxx.com/m/Jos/oauth.html"; $toUrl ="https://oauth.jd.com/oauth/token?grant_type=authorization_code&client_id=" .$appKey ."&client_secret=" .$appSecret ."&scope=read&redirect_uri=" .$url."&code=" .$code."&state=1234"; if(!$code){ //数据处理 此处其实是无法处理数据的,你问我,我问谁去啊?!!! echo 'hahahahhahahahah'; }else{ header("Location:".$toUrl); } } public function test(){ $appKey = 'DExxxxxxxxxxxxxxxxxxxxxx83'; $url = 'http://www.xxxx.com/m/Jos/oauth.html'; $toUrl = 'https://oauth.jd.com/oauth/authorize?response_type=code&client_id=' .$appKey.'&redirect_uri=' .$url.'&state=123'; header("Location:".$toUrl); } /** * 将获取到的token等信息 添加到数据库 下面的为获取的其中一次数据 注意时效性 */ public function addData(){ $data = array(); $data['access_token'] = '24xxxxxxxxxxxxxxxxxxxxae0'; $data['expires_in'] = '24xxxxxxxxxxxxxxxxxxxxxxxxe0'; $data['refresh_token'] = 'edxxxxxxxxxxxxxxxxxxxxxxxxxxx0f'; $data['time'] = '14xxxxx87475'; $model = new JosModel(); $res = $model->addData($data); echo $res; } /** * 查询京东快递物流跟踪信息 */ public function getTrace(){ //获取订单号 //$waybillCode = $_POST['waybillCode']; //事例京东订单号 $waybillCode = "23457562180"; //https://api.jd.com/routerjson 注:以后统一都使用https方式调用,之前使用http方式的请尽快切换一下入口地址。 Vendor('Jos.jd.request.EtmsTraceGetRequest'); $this->jd_client->appKey = $this->app_key; $this->jd_client->appSecret = $this->app_secret; $this->jd_client->accessToken = $this->access_token; $this->jd_client->serverUrl = $this->server_url;//SERVER_URL; $req = new \EtmsTraceGetRequest(); $req->setWaybillCode($waybillCode); $resp = $this->jd_client->execute($req, $this->jd_client->accessToken); var_dump($resp); } /** * 360buy.order.get 获取单个订单 */ public function getSingleOrder(){ Vendor('Jos.jd.request.OrderGetRequest'); $this->jd_client->appKey = $this->app_key; $this->jd_client->appSecret = $this->app_secret; $this->jd_client->accessToken = $this->access_token; $this->jd_client->serverUrl = $this->server_url; $req = new \OrderGetRequest(); //事例京东订单号 $waybillCode = "23457562180"; $req->setOrderId($waybillCode); //$req->setOptionalFields( "jingdong" ); //$req->setOrderState( "jingdong" ); $resp = $this->jd_client->execute($req, $this->jd_client->accessToken); var_dump($resp); } }