PHP code example of flofire / jos-sdk
1. Go to this page and download the library: Download flofire/jos-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
flofire / jos-sdk example snippets
use JDSDK;
class JDExample()
{
public $config = [
'appKey' => '123',
'appSecret' => '12345'
];
//搜索订单实例
public function orderSearch()
{
//Request(方法名)
$req = JDSDK::Request('OrderSearchRequest');
//传各种参数
$req->setPage(1);
$req->setPageSize(12);
$req->setStartDate(date('Y-m-d H:i:s', strtotime('-10 day')));
$req->setEndDate(date('Y-m-d H:i:s'));
$result = JDSDK::execute($req, 'xxxxxxtoken');
}
//认证实例
public function oauth()
{
$config = $this->config;
$redirect_uri = route('JDOauthCallback');
$state = '';
$oauthURL = "https://oauth.jd.com/oauth/authorize?response_type=code&client_id=".$config['appKey']."&redirect_uri=".$redirect_uri."&state=".$state;
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $oauthURL);
exit();
}
//认证回调用实例
public function oauthCallback(Request $requset)
{
$config = $this->config;
$code = $requset->input('code');
$state = (int)$requset->input('state');
$alert = '授权失败';
$redirect_uri = route('JDOauthCallback');
if ($code != ''){
//curl函数自行脑补
$result = curl([
'method' => 'POST',
'url' => "https://oauth.jd.com/oauth/token?grant_type=authorization_code&client_id=".$config['appKey']."&redirect_uri=".$redirect_uri."&code={$code}&state={$state}&client_secret=".$config['appSecret']
]);
//返回gb2312格式要进行处理
$json = json_decode(iconv('GB2312', 'UTF-8', $result));
if (isset($json->code) && $json->code == 0){
$userInfo = [
'uid' => $json->uid,
'nickname' => $json->user_nick,
//unix
'expire_time' => time() + $json->expires_in,
'access_token' => $json->access_token,
'refresh_token' => $json->refresh_token
];
$alert = '授权成功';
} else {
//{"code":"402","error_description":"缺少redirect_uri参数"}
}
}
return "<script>alert('$alert');window.opener=null;window.open('','_self');window.close();</script>";
}
}