20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In the above, we declare the method getPrice to be a Web service API by marking it with<br />

the tag @soap in its <strong>do</strong>c comment. We rely on <strong>do</strong>c comment to specify the data type of the<br />

input parameters and return value. Additional APIs can be declared in the similar way.<br />

Declaring Web Service Action<br />

Having defined the service provider, we need to make it available to clients. In particular,<br />

we want to create a controller action to expose the service. This can be <strong>do</strong>ne easily by<br />

declaring a CWebServiceAction action in a controller class. For our example, we will just<br />

put it in StockController.<br />

class StockController extends CController<br />

{<br />

public function actions()<br />

{<br />

return array(<br />

'quote'=>array(<br />

'class'=>'CWebServiceAction',<br />

),<br />

);<br />

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

//...return stock price for $symbol<br />

}<br />

That is all we need to create a Web service! If we try to access the action by URL http://<br />

hostname/path/to/index.php?r=stock/quote, we will see a lot of XML content which is<br />

actually the WSDL for the Web service we defined.<br />

Tip: By default, CWebServiceAction assumes the current controller is the service<br />

provider. That is why we define the getPrice method inside the StockController class.<br />

Consuming Web Service

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!