PHPwebservice instance _ PHP Tutorial

Source: Internet
Author: User
PHPwebservice instance. First, you need to briefly understand what webservice is. next we will give two very simple examples. webservice still cannot escape the server and client. In my testing environment: apache2.2.11, you should first understand what webservice is. Next, we will give two very simple examples. webservice cannot escape the server and client.

My testing environment is apache2.2.11 php5.2.10.

Before doing this test, make sure that the soap extension has been opened in your php configuration file, that is, extension = php_soap.dll;
OK. Now let's try webservice.

// Server-side serverSoap. php

$ Soap = new SoapServer (null, array ('uri '=> "http: // 192.168.1.179/"); // This uri is the ip address of the your SERVER.
$ 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 soap. php
Try {
$ Client = new SoapClient (null,
Array ('location' => "http: // 192.168.1.179/test/serverSoap. php", 'URL' => "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 a client calling a server function. let's create another class.

// ServerSoap. php on www.2cto.com server
$ ClassExample = array ();

$ Soap = new SoapServer (null, array ('URL' => "http: // 192.168.1.179/", 'classexample '=> $ classExample ));
$ Soap-> setClass ('chesterclass ');
$ Soap-> handle ();

Class chesterClass {
Public $ name = 'Chester ';

Function getName (){
Return $ this-> name;
}
}

// Client soap. php

Try {
$ Client = new SoapClient (null,
Array ('location' => "http: // 192.168.1.179/test/serverSoap. php", 'URL' => "http: // 127.0.0.1 /")
);
Echo $ client-> getName ();

} Catch (SoapFault $ fault ){
Echo "Error:", $ fault-> faultcode, ", string:", $ fault-> faultstring;
}

Author: Fox Warrior

Bytes. My testing environment is: apache2.2.11...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.