Saturday, December 28, 2013

yii 集成taobao php代码

1.下载php版taobao代码

下载地址:http://open.taobao.com/doc/detail.htm?id=112

2.程序目录结构

原始taobao结构:

image

修改后集成进yii的文件结构:

image

3.开始工作

1.把下载的程序结构解压,我放进了我的yii模块union中。

2.删除jushita、lotusphp_runtime目录

3.在top\目录下新建LtInflector.php、LtLogger.php两个文件

LtInflector.php 代码:

<?php
class LtInflector
{
public $conf = array("separator" => "_");

public function camelize($uncamelized_words)
{
$uncamelized_words = $this->conf["separator"] . str_replace($this->conf["separator"] , " ", strtolower($uncamelized_words));
return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $this->conf["separator"] );
}

public function uncamelize($camelCaps)
{
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $this->conf["separator"] . "$2", $camelCaps));
}
}

LtLogger.php 代码:

<?php
class LtLogger
{
public $conf = array(
"separator" => "\t"
);

public function log($logData)
{
if (is_array($logData))
{
$logData = implode($this->conf["separator"], $logData);
}
Yii::log($logData);
}
}




4.完成改造,调用示例:


protected\modules\union\controllers\TaobaoController.php代码:

<?php

if(!defined('TOP_SDK_WORK_DIR')){
define('TOP_SDK_WORK_DIR',dirname(__FILE__).'/../tmp/');
}

include 'taobao/TopSdk.php';

class TaobaoController extends CController
{
public $menuitems;
public $taobaoClient;

public function init(){
parent::init();
//$this->menuitems = Yii::app()->getModule('union')->menuitems;
return true;
}

public function actionIndex()
{
//将下载SDK解压后top里的TopClient.php第8行$gatewayUrl的值改为沙箱地址:http://gw.api.tbsandbox.com/router/rest,
//正式环境时需要将该地址设置为:http://gw.api.taobao.com/router/rest

$taobaoConfig = Yii::app()->getModule('union')->taobaoConfig;

//实例化TopClient类
$c = new TopClient;
if(defined('YII_DEBUG') && YII_DEBUG){
$c->appkey = $taobaoConfig['sandbox_appkey'];
$c->secretKey = $taobaoConfig['sandbox_secretKey'];
$c->sessionkey= $taobaoConfig['sandbox_sessionKey'];
$c->gatewayUrl = 'http://gw.api.tbsandbox.com/router/rest';
}else{
$c->appkey = $taobaoConfig['appkey'];
$c->secretKey = $taobaoConfig['secretKey'];
$c->sessionkey= $taobaoConfig['sessionKey'];
}

// $c->sessionkey= "test"; //如沙箱测试帐号sandbox_c_1授权后得到的sessionkey
$this->taobaoClient = $c;
$this->render('index',array(
'taobaoClient' => $c,
));
}
}

protected\modules\union\UnionModule.php代码:


<?php

class UnionModule extends CWebModule {

public $taobaoConfig = array();

/**
* @var array
* @desc Behaviors for models
*/
public $componentBehaviors = array();

public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'union.models.*',
'union.components.*',
'union.components.taobao.top.*',
'union.components.taobao.top.request.*',
'ext.bootstrap.widgets.*',
'ext.bootstrap.helpers.*',
'ext.bootstrap.behaviors.*',
));
}

public function getBehaviorsFor($componentName) {
if (isset($this->componentBehaviors[$componentName])) {
return $this->componentBehaviors[$componentName];
} else {
return array();
}
}

public function beforeControllerAction($controller, $action) {

if (parent::beforeControllerAction($controller, $action)) {
// this method is called before any module controller action is performed
// you may place customized code here
return true;
} else
return false;
}

/**
* @param $str
* @param $params
* @param $dic
* @return string
*/
public static function t($str = '', $params = array(), $dic = 'union') {
if (Yii::t("UnionModule", $str) == $str) {
return Yii::t("UnionModule." . $dic, $str, $params);
} else {
return Yii::t("UnionModule", $str, $params);
}
}

}
protected\modules\union\views\taobao\index.php代码:
<?php


//实例化具体API对应的Request类
$req = new UserSellerGetRequest;
$req->setFields("nick,user_id,type");
//$req->setNick("sandbox_c_1");

//执行API请求并打印结果
$resp = $taobaoClient->execute($req,$this->taobaoClient->sessionkey);
echo "result:";
print_r($resp);
echo "<br>";
//echo "nick:".$req->getNick();
?>



No comments: