Home | Mirror | Search

2. Soap

2.1. soapfunc.php

		
$ cat soapfunc.php
<?php

function reverse($str){

        $retval = '';

        if(strlen($str) < 1) {

                return new SoapFault('Client','','Invalid string');

        }

        for ($i = 1; $i <= strlen($str); $i++) {

                $retval .= $str[(strlen($str) - $i)];

        }

        return $retval;

}

function sum($num1, $num2) {

        if (trim($num1) != intval($num1)) {

                return new SoapFault('Client','','The first number is invalid');

        }

        if (trim($num2) != intval($num2)) {

                return new SoapFault('Client','','The second number is invalid');

        }

        return ($num1 + $num2);

}

function gettime(){

        $time=strftime("%Y-%m-%d %H:%M:%S");

        return $time;

}

?>
		
		
		

2.2. soapserver.php

		
$ cat soapserver.php
<?php
include_once('soapfunc.php');

$soap = new SoapServer(null,array('uri'=>"http://netkiller.6600.org/"));

$soap->addFunction('reverse');

$soap->addFunction('sum');

$soap->addFunction('gettime');

$soap->addFunction(SOAP_FUNCTIONS_ALL);

$soap->handle();

?>
		
		

2.3. soapclient.php

		
$ cat soapclient.php
<?php

try {

        $client = new SoapClient(null, array('location' =>"http://netkiller.6600.org/soapserver.php",'uri' => "http://netkiller.6600.org/"));

        $str = "This string will be reversed";

        $reversed = $client->reverse($str);

        echo "If you reverse '",$str,"', you get '",$reversed,"' </br>";

        $n1=50;

        $n2=130;

        $sum = $client->sum($n1,$n2);

        echo "If you try ",$n1,"+",$n2,", you will get ",$sum,"</br>";

        echo "The system time is: ",$client->gettime();

} catch (SoapFault $fault){

        echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;

}

?>
		
		
comments powered by Disqus