获取淘宝sessionkey 实时保存

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib_code.php');
#天猫配置
define('TMALL_APPKEY','你的key');
define('TMALL_SECRETKEY','你的appsecret');
define('TMALL_REDIRECT_URI','http://172.18.2.142/getsessionkey.php');
define('TMALL_RETURN_URI','Location:http://172.18.2.142/index.php');

$code = $_GET["code"];

if (!empty($_GET['code'])) {
$url = 'https://oauth.taobao.com/token';
$postfields= array(
'grant_type'=>'authorization_code',
'client_id'=>TMALL_APPKEY,
'client_secret'=>TMALL_SECRETKEY,
'code'=>$_GET['code'],
'redirect_uri'=>TMALL_REDIRECT_URI
);
$post_data = '';
foreach($postfields as $key=>$value){
$post_data .="$key=".urlencode($value)."&";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
//指定post数据
curl_setopt($ch, CURLOPT_POST, true);
//添加变量
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));
$output = curl_exec($ch);
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
$a=json_decode($output,true);


// $oOrder = &app::get('ome')->model('taobao_key');
$access_token = $a['access_token'];


//将token插入数据库
// $time = date('Y-m-d H:i:s',time());
// $sql = 'INSERT INTO dsc_taobao_sessionkey (`brand_id`,`time`, `access_token`) VALUES ("","'.$time.'","'.$access_token.'")';
// $db->query($sql);
$time = date('Y-m-d H:i:s',time());
$table = "dsc_taobao_sessionkey";
$field_values = array("time" =>$time, "access_token" =>$access_token);
$db->autoExecute($table, $field_values, "INSERT");


header(TMALL_RETURN_URI);
}else{
$url = "https://oauth.taobao.com/authorize";
$parames = array(
'client_id' =>TMALL_APPKEY,
'response_type' => 'code',
'redirect_uri'=>TMALL_REDIRECT_URI,
'state'=>'1'
);
$urlcode = '';
foreach ($parames as $key=>$value){
$urlcode .= "$key=".$value.'&';
}
$urlcode = substr($urlcode,0, strlen($urlcode)-1);

$authUrl = $url.'?'.$urlcode;
//echo $authUrl;die;
//header("Location:".$authUrl);
header("Location:".$authUrl);

}

 

我们访问 172.18.2.142/getsessionkey 时就获取了sessionkey ,将其存入数据库

转载于:https://www.cnblogs.com/wujf-myblog/p/9834763.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值