yii远程的webservices类,实现shop与sns的单点登录 <?php
class UserController extends Controller
{
/**
* Declares the 'ubeService' Web service action.
*/
public function actions()
{
return array(
'ubeService'=>array(
'class'=>'CWebServiceAction',
'classMap'=>array(
'User',
),
),
);
}
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()
{
$this->outputMessage("passport api.");
}
/**
* This is the action to handle external exceptions.
*/
public function actionError()
{
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest) {
echo $error['message'];
} else {
echo $error['message'];
}
Yii::app()->end();
$this->render('error', $error);
}
}
/**
* @param string account
* @return array account data
* @soap
*/
public function getAccount($account)
{
if (empty($account)) {
$this->data['error'] = 2;
$this->data['message'] = 'Email address or Mobile number is required.';
return $this->data;
}
$type = $this->getType($account);
if (!$type) {
$this->data['error'] = 3;
$this->data['message'] = 'Account should be an Email address or a Mobile number.';
return $this->data;
}
$service = UserService::getinstance();
if ($data = $service->get($account, $type)) {
$this->data['error'] = 0;
$this->data['data'] = $data;
} else {
$this->data['error'] = 1;
$this->data['data'] = $data;
$this->data['message'] = 'Account not exist.';
}
return $this->data;
}
}?>
上面的类来自passport项目,简单的封装了获取用户信息的类,passport项目为实现shop与sns间的中间件。
<?php
/**
* Passport Universal SDK
*/
class Passport {
public $data = array();
public $error = 1;
public $message = "";
private $service = "";
public function __construct()
{
$this->service = "http://xxxx/index.php?r=user/ubeService";
}
/**
* 获取用户数据。
**/
public function getAccount($account)
{
$client = new SoapClient($this->service);
$ret = $client->getAccount($account);
if ($ret['error'] == 0) {
$this->data = $ret['data'];
return $this->data;
} else {
$this->setError($ret);
}
return false;
}
}