How to create WebService


(vitocourt) #1

Hi guys,

I’m configuring my server for webservice but I’m have a problem.
I don´t understand how and where create my wsdl file.

I made an controller example but I don´t send an array like parameter…

My controller is

<?php
class WebServiceController extends CController {
   public function actions() {
        return array(
            'wreader' => array(
                'class' => 'CWebServiceAction',
            ),
        );
    }

    /**
     * @param string username
     * @return float
     * @soap
     */
    public function getauth($uname) {
        if ($uname == 'harpreet' && $pass == '123456'){
            $session = Yii::app()->session;
            $session['u_id'] = 1111;
            return 1;
        } else {
            return 'Usuario o contraseña no son válidos';
        }
    }

     /**
     * @param string username
     * @return float
     * @soap
     */
    public function getemp($uname) {
        $session = Yii::app()->session;
        return isset($session['u_id'])?$session['u_id']:999;
    }
}

?>

My php file to call the service is

<?php
	$arreglo = new Users();
	$arreglo->uname = 'harpreet';
	$arreglo->pass='123456';
	$arreglo->shop='1';

	$client=new SoapClient('http://myurl.com/prueba/index.php/wreader');
	echo "\n".$client->getauth($arreglo);
	echo "\n".$client->getemp('harpreet');


	class Users {
		public $uname;
		public $pass;
		public $shop;
	}

?>

This is the error

Catchable fatal error : Object of class Users could not be converted to string in C:\wamp\www\myphp.php on line 8


(vitocourt) #2

After some change, my code show this error

<?php
class WebServiceController extends CController {
   public function actions() {
        return array(
            'wreader' => array(
                'class' => 'CWebServiceAction',
            ),
        );
    }

    /**
     * @param  string tipo_documento
     * @param  string id
     * @param  string type
     * @param  string get_info
     * @param  string regimen
     * @return string the statement
     * @soap
     */
    public function sendCustomer($tipo_documento, $id, $type, $get_info, $regimen) {
    //
        $model = new Customer();
        $model->tipo_documento = $tipo_documento;
        $model->id = $id;
        $model->type = $type;
        $model->get_info = $get_info;
        $model->regimen = $regimen;
        $yesno = $model->save();

        if ($yesno=TRUE) {
            //on success
            return 'Record Saved';
        }
        else {
            //on failure
            return $yesno;
        }
    }   
}

?>

My php plain is

<?php
$client=new SoapClient('http://myUrl.com/prueba/index.php/webService/wreader',array('soap_version'=>SOAP_1_2,'trace'=>true)
	);
$client->sendCustomer('31','1765456234', '1','1','2');
echo $client->__getLastRequest().'<br>';
echo $client->__getLastResponse().'<br>';
?>

The server response is

Fatal error: Uncaught SoapFault exception: [Sender] Function (“sendCustomer”) is not a valid method for this service in C:\wamp\www\myphp.php:9 Stack trace: #0 C:\wamp\www\myphp.php(9): SoapClient->__call(‘sendCustomer’, Array) #1 C:\wamp\www\myphp.php(9): SoapClient->sendCustomer(‘31’, ‘1102363258’, ‘1’, ‘1’, ‘2’) #2 {main} thrown in C:\wamp\www\myphp.php on line 9

SoapFault: Function (“sendCustomer”) is not a valid method for this service in C:\wamp\www\myphp.php on line 9

How i can solve this error?