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

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

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

    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.参考如下获取某一个物流单号的物流信息

    

使用京东授权,你需要先在京东开发者平台上注册并创建应用。接下来,你需要获取应用的App Key和App Secret,这是你的应用与京东授权服务器进行通信的凭证。 在获取授权之前,你需要在应用中添加一个回调URL。这个URL将在用户授权后重定向到你的应用程序。你可以在京东开发者平台上的应用设置中设置回调URL。 一旦你的应用程序准备好了,你就可以开始获取授权了。你可以使用京东开发者平台上提供的SDK,或者自己编写代码来实现授权。 以下是使用PHP-SDK获取授权示例代码: ```php <?php require_once 'jd_sdk/jd.php'; $app_key = "your_app_key"; $app_secret = "your_app_secret"; $redirect_uri = "your_callback_url"; $auth = new OAuth($app_key, $app_secret); $auth->setRedirectUri($redirect_uri); if (isset($_GET['code'])) { $code = $_GET['code']; $access_token = $auth->getAccessTokenByCode($code); // 授权成功,你可以保存access_token并使用它来访问京东API } else { $auth_url = $auth->getAuthorizeUrl(); header("Location: $auth_url"); } ``` 在这个示例中,我们使用PHP-SDK获取授权。`$app_key`和`$app_secret`是你在京东开发者平台上创建应用时获得的。`$redirect_uri`是你在应用设置中设置的回调URL。 如果用户还没有授权,我们将重定向到授权页面。如果用户已经授权,我们将从回调URL中获取授权码,并使用它来获取访问令牌。完成后,你可以使用访问令牌来访问京东API。 希望这个示例对你有所帮助!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值