PHP

php Implementation of webservice Instance


In this paper, the method of realizing webservice by php is described with examples. Share it for your reference. The specific implementation method is as follows:

First of all, we should simply understand what webservice is. Next, we will do two very simple examples. webservice still cannot escape server and client.

The test environment here is: apache2.2. 11 php5.2. 10

Before doing this test, make sure that the soap extension is turned on in your php configuration file, that is,

extension=php_soap.dll;

OK Now let’s experience webservice

server end serverSoap. php

$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179/"));//This uri is your SERVER ip.
$soap->addFunction('minus_func');                                                 //Register the function
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
function minus_func($i, $j){
    $res = $i - $j;
    return $res;
}
//client End  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 client calling server-side functions, and we will make an class.

server end 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 End  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;
}

I hope this article is helpful to everyone’s PHP programming.