yii利用SoapClient调用webservices

本文介绍如何在Yii框架中使用SoapClient类来调用外部的Webservices,通过实例展示了在passport项目中封装获取用户信息的过程,该项目作为shop与sns之间的通信桥梁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值