以为公司祖传代码太多使用的PHP5.4.45版本,所以无法使用SDK包,只能原生自己写,踩了不少坑。
1.授权
https://oauth.taobao.com/authorize?response_type=code&client_id=123456&redirect_uri=http://127.0.0.11/taobao.php&state=12312&view=web
我这里使用的本地地址测试的,正式环境,所以回调修改为自己的,还有client_id改为自己的App Key就好,应该没有什么问题
2.获取access_token
//获取access_token function access_token_get(){ $data[\'code\'] = \'*******************************\'; $data[\'client_id\'] = \'********\'; $data[\'client_secret\'] = \'*******************************\'; $data[\'redirect_uri\'] = \'http://127.0.0.11/taobao.php\'; $data[\'grant_type\'] = \'authorization_code\'; $data[\'state\'] = \'12312\'; $data[\'view\'] = \'web\'; $url =\'https://oauth.taobao.com/token\'; $result_token = curl($url,$data); $tokenPath = "taobao/".$data[\'client_id\'].".json";//文件名字 $fp = fopen($tokenPath, "w"); fwrite($fp, json_encode($result_token)); fclose($fp); }
这里参数继续换成自己的,code使用授权后地址栏返回的,这里要注意一个坑,使用自己写的curl有时候会出问题,暂时不清楚哪里问题所以使用了SDK自带的curl
function curl($url, $postFields = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // if ($this->readTimeout) { // curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); // } // if ($this->connectTimeout) { // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); // } //https 请求 if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($postFields) && 0 < count($postFields)) { $postBodyString = ""; $postMultipart = false; foreach ($postFields as $k => $v) { if("@" != substr($v, 0, 1))//判断是不是文件上传 { $postBodyString .= "$k=" . urlencode($v) . "&"; } else//文件上传用multipart/form-data,否则用www-form-urlencoded { $postMultipart = true; } } unset($k, $v); curl_setopt($ch, CURLOPT_POST, true); if ($postMultipart) { curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); } } $reponse = curl_exec($ch); if (curl_errno($ch)) { throw new Exception(curl_error($ch),0); } else { $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // if (200 !== $httpStatusCode) // { // throw new Exception($reponse,$httpStatusCode); // } } curl_close($ch); return $reponse; }
3.到这里就获取完毕了,下面刷新按照文档所说是通过授权获取的refresh_token(前置条件:re_expires_in>0),可用来刷新access token 的r2时长。
这里我使用的App Key不知为何返回的re_expires_in一直为0暂时无法解决,已提交工单,后续有结果会更新上来
header("Content-Type:text/html;charset=UTF-8");
date_default_timezone_set("Asia/Shanghai");
require_once \'taobao/qm.php\';
$appKey = \'你的APP key\';
$appSecret = \'***************************\';
$paramArr = array(
\'app_key\' => $appKey,
\'format\' => \'json\',
\'method\' => \'taobao.top.auth.token.refresh\',
//\'grant_type\' => \'refresh_token\',
\'refresh_token\' => \'****************************************\',
\'client_id\' => $appKey,
\'client_secret\' => $appSecret,
\'simplify\' => true,
\'sign_method\'=>\'md5\',
\'timestamp\' => date(\'Y-m-d H:i:s\'),
\'v\' => \'2.0\'
);
//生成签名
$sign = createSign($paramArr, $appSecret);
//组织参数
$strParam = createStrParam($paramArr);
$strParam .= \'sign=\'.$sign;
$url = \'https://eco.taobao.com/router/rest?\'.$strParam;
$result_token = curl($url,$postfields);
签名函数createSign代码
//签名函数 function createSign ($paramArr,$appSecret) { $sign = $appSecret; ksort($paramArr); foreach ($paramArr as $key => $val) { if ($key != \'\' && $val != \'\') { $sign .= $key.$val; } } $sign.=$appSecret; $sign = strtoupper(md5($sign)); return $sign; }
这里刷新和请求接口是使用的一种方法,这里有个坑,在沙箱环境不会出现,但是正式环境会出现,我踩了一下午才踩平,组装的$paramArr里面的数组必须安装ASCII码表排序。不然会报错,签名错误。
补充,关于使用的App Key不知为何返回的re_expires_in一直为0的问题经过相关技术人员回复处理发现是淘宝方问题,有部分App Key是re_expires_in返回就为0的,但是access token的过期时间为一年,暂时够用,但是这个真的坑,总不能一年去授权一次吧,那天系统突然崩溃也不知道啥情况,希望官方能解决下。
这里补充一个遇到的问题,就是查询买家信息之类的都是带***的经过加敏处理模糊化的信息,这里只有用聚石塔服务器请求接口产能返回完整信息。
暂时就这些内容,如有问题欢迎留言