京东jos 获取授权及php-sdk的使用示例

news/2024/10/22 17:20:07/

    背景:项目需要使用京东的物流服务,中间各种交流、签合同过程不做赘述,作为程序员,凭什么总要依靠代码实现能力来判断,鬼知道自己哪天是什么样子,以后不做程序猿,也是一条好汉!可惜,口水吐完还得老老实实来搬砖。

    其实,很不喜欢泛泛而谈,以下是实现授权的操作流程,仅做参考。

    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);

    }
}

    5.参考如下获取某一个物流单号的物流信息

    


http://www.ppmy.cn/news/856506.html

相关文章

Bluetooth Profiles list-HFP HSP A2DP AVRCP PBAP MAP profiles

转载: Bluetooth Profiles list-HFP HSP A2DP AVRCP PBAP MAP profiles (rfwireless-world.com) This page covers list of bluetooth profiles. The list include HFP profile, HSP profile, A2DP profile, AVRCP profile, PBAP profile and MAP bluetooth profiles. The page…

前后端交互数据格式之form-data和json

前后端数据交互的方式 form-data格式json字符串格式 一、form-data格式 请求头为 application/x-www-form-urlencoded 对应的请求数据格式就是form-data格式数据格式为 : usernamexiaohu&password123456默认情况下&#xff0c;axios会将JavaScript对象序列化为JSON所以我…

低延迟长续航手游伴侣—HyperX Cloud Buds云雀蓝牙无线游戏耳机

前一阵火爆的HyperX Cloud Buds TWS真无线耳机&#xff0c;让小小的云雀又出圈了一把。讲真&#xff0c;极度未知HyperX云雀系列真的是宝藏产品&#xff0c;符合人体耳朵贴合度的入耳式电竞耳机&#xff0c;从有线、无线到真无线&#xff0c;一步步在升级换代。其中&#xff0c…

jshop商城

下载源代码 git clone https://git.oschina.net/dinguangx/jshop.git 使用maven编译并生成eclipse/idea项目结构 mvn compile -Dmaven.test.skiptrue mvn eclipse:eclipse 此步骤中&#xff0c;需要联网下载依赖的jar包&#xff0c;可能会比较慢 导入到eclipse中 在eclipse中&a…

音乐流媒体服务器Navidrome

什么是 Navidrome &#xff1f; Navidrome 是一个开源的基于网络的音乐收藏和流媒体服务器&#xff0c;与 Subsonic/Airsonic 兼容。它让您可以自由地从任何浏览器或移动设备收听您的音乐收藏。就像您的个人 Spotify&#xff01; 什么是 Spotify &#xff1f; Spotify 是一个正…

C++ 迭代器的设计与使用

C 迭代器是一种用于访问容器&#xff08;例如数组、向量、列表等&#xff09;元素的工具。它们允许我们以一种统一的方式遍历和操作容器中的数据&#xff0c;而不用关心容器内部数据结构的细节 C 迭代器 std 重要函数 std::begin 和 std::endstd::advance(iter,n)向前移动…

让yahoo邮箱支持pop

yahoo信箱POP设置 邮件服务器设置如下&#xff1a; 接收邮件&#xff08;POP3&#xff09;服务器&#xff1a;pop.mail.yahoo.com.cn 发送邮件&#xff08;SMTP&#xff09;服务器&#xff1a;smtp.mail.yahoo.com.cn 注意&#xff1a;雅虎中文缺省是不提供POP access服务的。…

魔百盒CM101S-2/咪咕MG100-支持多种无线-Hi3798MV100-免拆机卡刷固件包

魔百盒CM101S-2&#xff0f;咪咕MG100_rda5995,7601,9083,8188等无线-Hi3798MV100-免拆机卡刷固件包 特点&#xff1a; 1、适用于对应型号的电视盒子刷机&#xff1b; 2、开放原厂固件屏蔽的市场安装和u盘安装apk&#xff1b; 3、修改dns&#xff0c;三网通用&#xff1b; …