Php implements webservice instances, phpwebservice instance _ PHP Tutorial

Source: Internet
Author: User
Php implements webservice instances and phpwebservice instances. Php implements webservice instance. phpwebservice instance this article describes how php implements webservice. Share it with you for your reference. The specific implementation method is as follows: first, you must implement a webservice instance in php and a phpwebservice instance in a simple way.

This example describes how to implement webservice in php. Share it with you for your reference. The specific implementation method is as follows:

First, let's take a simple look at what webservice is. Next, let's take two very simple examples: webservice cannot escape the server and client.

The test environment is apache2.2.11 php5.2.10.

Before doing this test, make sure that the soap extension has been enabled in your php configuration file, that is

The code is as follows:

Extension = php_soap.dll;


OK. Now let's try webservice.

Server serverSoap. php

The code is as follows:

$ 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.

Server serverSoap. php

The code is as follows:

$ 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;
}

I hope this article will help you with PHP programming.


Who can give a php webservice example?

The basic idea of Web Service is to use XML-based HTTP remote calls to provide a standard mechanism for communication between heterogeneous systems, this eliminates the need to establish a new protocol. At present, there are two protocol standards for Web Service communication, one is XML-RPC, the other is SOAP. XML-RPC is relatively simple, the appearance of time is relatively early, SOAP is more complex, mainly in some need of stability, robustness, security and complex interaction when using.

PHP integrates access to both XML-RPC and SOAP protocols, both of which are concentrated in xmlrpc extensions. In addition, in php pear, whether PHP 4 or PHP 5, has been integrated with the XML-RPC extension by default, and the extension has nothing to do with xmlrpc extension, can independently implement XML-RPC protocol interaction, if there is no xmlrpc extension, we recommend using the PEAR: XML-RPC extension.

All that is said is virtual. the following example shows everything.

First, construct the webservice server:

On the server side, the function I defined is get ("helloworld"); // hello is the input parameter.

/**
* Function: the function provided to the RPC client.
* Parameters:
* $ Function to be called by the method client
* $ Params parameter array of the function to be called by the client
* Return: return the specified call result.
*/
Function rpc_server_func ($ method, $ params ){
$ Parameter = $ params [0]; // name of the input function

$ Parameter1 = $ params [1]; // input parameters
If ($ parameter = "get ")
{
$ Return = "This data by get method". $ parameter1;
}
Else
{
$ Return = "Not specify method or params ";
}
Return $ return;
}

// Generate a XML-RPC on the server side
$ Xmlrpc_server = xmlrpc_server_create ();

// Register a method called by the server, rpc_server, which actually points to the rpc_server_func function
Xmlrpc_server_register_method ($ xmlrpc_server, "rpc_server", "rpc_server_func ");

// Accept XML data POST from the client
$ Request = $ HTTP_RAW_POST_DATA;

// Execute the XML request to call the client and obtain the execution result
$ Xmlrpc_response = xmlrpc_server_call_method ($ xmlrpc_server, $ request, null );

// Output the result XML after function processing
Header ("Content-Type: text/xml ");
Echo $ xmlrpc_response;

// Destroy XML-RPC server resources
Xmlrpc_server_destroy ($ xmlrpc_server );
?>

Use php to access the defined webse ...... the remaining full text>
 

How to call webservice instances in PHP reference _ PHP Tutorial

The code is really simple. when creating a SoapClient object, you can save it in a local WSDL file or use a remote address. many parameters can be included in the array below, for specific parameters, check the SoapClient help of php. here, the character set encoding is used. if the parameters of the method to call contain Chinese characters, you must specify the character set encoding. Otherwise, an error occurs.

Examples in this article describes how to implement webservice in php. Share it with you for your reference. The specific implementation method is as follows: first, you must be simple...

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.