如何在yii中创建web服务?
问题描述:
我是Yii框架的新手,所以在yii中需要一些帮助来重写我的php/soap web服务。 我发现Yii为此提供了CWebService。如何在yii中创建web服务?
我创建SOAP服务器就像在此之前:
$server = new SoapServer("WebServiceWSDL.wsdl");
$server->addFunction('myfunction');
$server->handle();
function myfunction(){ ...
}
我如何使用CWebService? 简单示例的一些帮助对于开始很有帮助。
答
尝试这样的事情
class WebServicesForUserController extends Controller
{
public function actions()
{
// here you need to mention the class for the web service
return array(
'services'=>array(
'class'=> 'CWebServiceAction',
)
);
}
// these comments are used by Yii to create wsdl for you. The following functions is a webservice
/**
*
* @return datetime this is the current timestamp of the server
* @soap
*/
public function giveTimestamp()
{
$query='select current_timestamp();';
$record= Yii::app()->db->createCommand($query)->queryScalar();
return $record;
}
// this action is created to consume the webservice.
If you will visit this action then you will get the timestamp which is sent to you as a webservice response
public function actionLets()
{
ini_set ('soap.wsdl_cache_enable' , 0); ini_set ('soap.wsdl_cache_ttl' , 0);
$wsdlURL='http://localhost/project/webServicesForUser/services';
$client=new SoapClient($wsdlURL);
$result=$client->giveTimestamp();
echo $result;
}
}
注: - 如果你想看到的WSDL只是去到你的浏览器,用于创建SOAP对象的URL
如何使用CWebService与我自己的wsdl?你能举个例子吗? – Jamol