Home > Article > Backend Development > PHP webservice example_PHP tutorial
First of all, everyone needs to briefly understand what webservice is. Next, we will give two very simple examples. Webservice still cannot escape the server side and client side.
The environment I tested is: apache2.2.11 php5.2.10
Before doing this test, make sure that the soap extension has been turned on in your php configuration file, that is, extension=php_soap.dll;
OK now let’s experience webservice
//server side serverSoap.php
$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179/"));//This uri is your SERVER ip.
$soap->addFunction('minus_func');
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
function minus_func($i, $j){
$res = $i - $j;
Return $res;
}
//client side clientSoap.php
try {
$client = new SoapClient(null,
array('location' =>"http://192.168.1.179/test/serverSoap.php",'uri' => "http://127.0.0.1/")
);
echo $client->minus_func(100,99);
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
This is an example of the client calling a server-side function. Let’s create a class.
//www.2cto.com server-side serverSoap.php
$classExample = array();
$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179/",'classExample'=>$classExample));
$soap->setClass('chesterClass');
$soap->handle();
class chesterClass {
Public $name = 'Chester';
Function getName() {
return $this->name;
}
}
//client side clientSoap.php
try {
$client = new SoapClient(null,
array('location' =>"http://192.168.1.179/test/serverSoap.php",'uri' => "http://127.0.0.1/")
);
echo $client->getName();
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
Author Fox Hero