1.创建一个Yii应用程序

2.在protected/controllers文件夹下添加一个WeatherController.php,用来模拟一个根据城市名查询天气的接口
值得注意的是,Yii依靠代码注释(@param,@return,@soap)自动生成WSDL(WebService Description Language),以便对外暴露WebService接口

<?php
/**
 * Weather api
 *
 * @author Aiddroid
 */
class WeatherController extends CController {

    public function actions() {
	return array(
	    'api' => array(
		'class' => 'CWebServiceAction'
	    )
	);
    }

    /**
     * 
     * @param string $cityName
     * @return array waether infos
     * @soap declare a soap action
     */
    public function query($cityName) {
	return array(
	    'cityName' => $cityName,
	    'temp' => 20,
	    'desc' => 'Sunny',
	);
    }

}

 

3.客户端调用代码,跟调用本地接口一样!

<?php
$wsdl = 'https://dev.com/yiidemo/index.php?r=weather/api';
$soap = new SoapClient($wsdl);
$weather = $soap->query('Beijing');

var_dump($weather);

4.输出结果

array(4) { ["cityName"]=> string(7) "Beijing" ["temp"]=> int(20) ["desc"]=> string(5) "Sunny" }

 

Post Navigation