Presentation

GWebService is a set of Yii components designed to deal with web service : they could be used to write Soap Client or Soap Server. They are inspired by Yii Web Service work. The main difference is the SOAP library used : Yii Web Service requires PHP SOAP extension, GWebService use NuSoap library (included version has been amended).

GWebService consists :

  • a SOAP server class : GSoapServer
  • a SOAP server action class : GSoapServerAction
  • a SOAP client class : GSoapClient

Defining Web Service provider

Like Yii core Web Service feature, the Service Provider class defines method that could be remotely invoked. GWebService relies on doc comment to identify which method could be invoked, what are their parameters and return type.

You need to comment method using phpDocumentor syntax and add @soap tag in doc comment block.

class StockProvider {
    /**
     * @param string the symbol of the stock
     * @return float the stock price
     * @soap
     */
    public function getPrice($symbol)
    {
        $symbol = strtolower($symbol);
        $prices=array('banana'=>2.20, 'apple'=>2.50, 'pear' => 2.90, 'lemon' => 0.50);
        return isset($prices[$symbol])?$prices[$symbol]:0;
    }
}

Declaring an action

To allow remote invocation, we need to add a Web Server action to a controller. We have to declare a GWebServerAction on a Controller and set provider attribute to provider class (or object instance).

If provider is not set, current controller is used as service provider.

Regarding example above, we can define StockController as is :

class StockController extends CController {
    public function actions() {
      return array(
          'index' => array(
              'class'    => 'ext.GWebService.GSoapServerAction',
              'provider' => 'StockProvider',
          ),
      );
	}
}

Controller could be used directly as provider :

class Stock2Controller extends CController {
    public function actions() {
      return array(
          'index' => array(
              'class'    => 'ext.GWebService.GSoapServerAction',
          ),
      );
	}
	
    /**
     * @param string the symbol of the stock
     * @return float the stock price
     * @soap
     */
    public function getPrice($symbol)
    {
        $symbol = strtolower($symbol);
        $prices=array('banana'=>2.20, 'apple'=>2.50, 'pear' => 2.90, 'lemon' => 0.50);
        return isset($prices[$symbol])?$prices[$symbol]:0;
    }
}

Consuming Web Service

You can use GSoapClient class for consuming web service. You can give WSDL URL or specify endpoint URL.

If both WSDL URL and specific endpoint URL are set, specific endpoint is considered.

Anywhere in your code, you can use one of this snippet to execute remote method.

$client = Yii::createComponent(array(
	'class' => 'ext.GWebService.GSoapClient',
	'wsdlUrl' => http://yii.guenot.info/index.php?r=stock));
$price = $client->call('StockProvider.getPrice', array('lemon'));
echo "lemon cost: " . $price . " €/kg";
// lemon cost: 0.5 €/kg

or

$client = Yii::createComponent(array(
	'class' => 'ext.GWebService.GSoapClient',
	'serviceUrl' => http://yii.guenot.info/index.php?r=stock&ws=1));
$price = $client->call('StockProvider.getPrice', array('lemon'));
echo "lemon cost: " . $price . " €/kg";
// lemon cost: 0.5 €/kg

or

$client = Yii::createComponent(array(
	'class' => 'ext.GWebService.GSoapClient',
	'wsdlUrl' => http://yii.guenot.info/index.php?r=stock2));
$price = $client->call('Stock2Controller.getPrice', array('APPLE'));
echo "APPLE cost: " . $price . "€/kg";
// APPLE cost: 2.5€/kg

or

$client = Yii::createComponent(array(
	'class' => 'ext.GWebService.GSoapClient',
	'serviceUrl' => http://yii.guenot.info/index.php?r=stock2&ws=1));
$price = $client->call('Stock2Controller.getPrice', array('APPLE'));
echo "APPLE cost: " . $price . "€/kg";
// APPLE cost: 2.5€/kg
TOC