O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Web Service<br />
Web service is a software system designed to support interoperable machine-to-machine<br />
interaction over a network. In the context of Web applications, it usually refers to a set of<br />
APIs that can be accessed over the Internet and executed on a remote system hosting the<br />
requested service. For example, a Flex-based client may invoke a function implemented<br />
on the server side running a PHP-based Web application. Web service relies on SOAP as<br />
its foundation layer of the communication protocol stack.<br />
<strong>Yii</strong> provides CWebService and CWebServiceAction to simplify the work of implementing<br />
Web service in a Web application. The APIs are grouped into classes, called service<br />
providers. <strong>Yii</strong> will generate for each class a WSDL specification which describes what APIs<br />
are available and how they should be invoked by client. When an API is invoked by a<br />
client, <strong>Yii</strong> will instantiate the corresponding service provider and call the requested API to<br />
fulfill the request.<br />
Note: CWebService relies on the PHP SOAP extension. Make sure you have enabled<br />
it before trying the examples displayed in this section.<br />
Defining Service Provider<br />
As we mentioned above, a service provider is a class defining the methods that can be<br />
remotely invoked. <strong>Yii</strong> relies on <strong>do</strong>c comment and class reflection to identify which methods<br />
can be remotely invoked and what are their parameters and return value.<br />
Let's start with a simple stock quoting service. This service allows a client to request for<br />
the quote of the specified stock. We define the service provider as follows. Note that we<br />
define the provider classStockController by extending CController. This is not required. We<br />
will explain why we <strong>do</strong> so shortly.<br />
class StockController extends CController<br />
{<br />
/**<br />
* @param string the symbol of the stock<br />
* @return float the stock price<br />
* @soap<br />
*/<br />
public function getPrice($symbol)<br />
{<br />
$prices=array('IBM'=>100, 'GOOGLE'=>350);<br />
return isset($prices[$symbol])?$prices[$symbol]:0;<br />
//...return stock price for $symbol<br />
}<br />
}