<?php
/*
* 处理回调参数$top_parameters
* 将其转换成数组,并可用get型函数各参数的值
*/
/**
* Description of Parameter
*
* @author 张永军
*/
class Parameter {
    
        private $parametersArray;//top_parameters解析后存在此数组中
        public function __construct($top_parameters) {  
                //$paramArray[]=new ArrayObject;
                $param = explode('&', base64_decode($top_parameters));
                foreach ($param as $key => $value) {
                        $s=explode('=', $value);
                        $paramArray[$s[0]]=$s[1];
                }
                $this->parametersArray= $paramArray;
        }
        
        public function getParamArray()
        {
                return $this->parametersArray;
        }
        
        public function getVisitorId()
        {
                return $this->parametersArray['visitor_id'];
        }
        
        public function getVisitorNick()
        {
                return $this->parametersArray['visitor_nick'];
        }
        
         public function getVisitorRole()
        {
                return $this->parametersArray['visitor_role'];
        }  
}
?>

<php
$top_appkey = $_GET['top_appkey'];
$top_parameters = $_GET['top_parameters'];
$top_session = $_GET['top_session'];
$top_sign = $_GET['top_sign'];

$secret = 'xxxxxxx'; // 别忘了改成你自己的

$md5 = md5( $top_appkey . $top_parameters . $top_session . $secret, true );
$sign = base64_encode( $md5 );

if ( $sign != $top_sign ) {
echo "signature invalid.";
exit();
}

$parameters = array();
parse_str( base64_decode( $top_parameters ), $parameters );

$now = time();
$ts = $parameters['ts'] / 1000;
if ( $ts > ( $now + 60 * 10 ) || $now > ( $ts + 60 * 30 ) ) {
echo "request out of date.";
exit();
}

echo "welcome {$parameters['visitor_nick']}.";
>

Taoapi_Php_Sdk_V2.1是什么东西


在这里我给大家说一下该SDK的好处和用法.


好处:


1.支持1.0 和 2.0 API的调用

2.支持Array Json Xml 三种格式的返回

3.支持按小时来缓存API数据

4.API获取不到数据或报错时自动记录日志

5.在网速繁忙时一次获取不到数据会自动重试,默认重试三次

6.支持中文错误提示

7.完美支持目前的1.0 和2.0的md5 hmac 算法


更多的惊喜,谁用谁知道....


文件说明;


1. Taoapi_Config.php 全局性的配置文件,单例模式

函数:

setTestMode( ) //设置API的环境 沙箱或正式

setVersion () //设置API版本 1或2

setAppKey() //设置appkey

setAppSecret() //设置AppSecret


2. Taoapi_Cache.php 读写文件缓存文件

函数:

//设置数据缓存的时间,单位:小时;0表示不缓存,不设置默认为 0

setCacheTime(6);

//设置缓存保存的目录.不设置默认为: ./Apicache

setCachePath('./Apicache');


3. Taoapi_Exception.php 错误文件,返回中英错误信息,自动写入日志

4. Taoapi_Util.php HTTP Url 提交文件,这个与以前官方提供Snoopy.php的区别不大

5.Taoapi.php API入口操作文件.也是最重要的文件


函数:

//1:1.0API版,2:2.0版 不设置默认为1, 这里的版本只作用处当前的API,但优先级比 Taoapi_Config 里的高

$Taoapi->setVersion(1);

//设置API读取失败时重试的次数,可以提高API的稳定性,不设置默认为3次

$Taoapi->setRestNumberic(3);

//获取当前的Sign码

$Taoapi->getSign();

//返回API参数

$Taoapi->getParam();

//获取带参数提交到淘宝的URL地址

$Taoapi->getUrl();

//关闭错误提示

$Taoapi->setCloseError();//关闭后错误将写入到log目录下的日志文件中

//获取错误信息,支持中文

$Taoapi->getErrorInfo();


示例:


include_once 'Taoapi.php';

$Taoapi_Config = Taoapi_Config::Init();

$Taoapi_Config->setTestMode(false) //true:沙箱环境 false:正式环境

->setAppKey(12001516)

->setAppSecret('de685c1b440478770049c73c8d115d46');


$Taoapi = new Taoapi;

//搜索商品信息(taobao.items.search)

$Taoapi->method = 'taobao.items.search';

$Taoapi->session = '230531a8bc1f5513d5c9b401a93d87cc25d42';

$Taoapi->fields = 'iid,detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str';

$Taoapi->q = '手机';


//这里将得到查询手机商品的数据

$TaoapiItem = $Taoapi->Send('get','xml')->getArrayData();


//得到单个用户信息(taobao.user.get)

$Taoapi->method = 'taobao.user.get';

$Taoapi->session = '230531a8bc1f5513d5c9b401a93d87cc25d42';

$Taoapi->fields = 'user_id,nick,sex,buyer_credit,seller_credit,location.country,created';

$Taoapi->nick = '浪子arvin';


//这里将得到查询用户 浪子arvin 的数据

$TaoapiUser = $Taoapi->Send('get','xml')->getArrayData();


更新日记:

2009-12-29 PHPSDK版本号由2.0升级到2.1

2009-12-29 在Taoapi_Config中同样加入setVersion方法,支持设置全局性的API版本号

2009-12-29 升级API2.0生成sign方式,完美支持官方提出的MD5和HMAC二种方式

2009-12-28 加入错误日志.当关闭错误显示时自动记录到错误日志中

2009-12-25 修正PHP5.3中出现URL拼接不一致的BUG

2009-12-22 发布Taoapi_Php_Sdk_V2.0