Java calls PHP WebService

Source: Internet
Author: User

1. First download the PHP WebService package: nusoap, and download it on the official website. The link is not provided. go to Google.
Based on nosoap, we wrote a PHP WebService server. The example is as follows:

<? PHP
Header ("Content-Type: text/html; charset = UTF-8 ");
Require ('../lib/nusoap. php ');

$ Server = new soap_server ();
$ Server-> configurewsdl ('hellowsdl ', 'urn: hellowsdl ');
$ Server-> WSDL-> schematargetnamespace = 'urn: hellowsdl ';

$ Server-> Register ('hello', // method name
Array ('name' => 'xsd: string'), // input parameters
Array ('Return '=> 'xsd: string'), // output parameters
'Urn: hellowsdl ', // namespace
'Urn: hellowsdl # hellowsdl ', // soapaction
'Rpc ', // Style
'Encoded', // use
'Says hello to the caller' // documentation
);

Function Hello ($ name ){
If ($ name = ''){
Return new soap_fault ('client', '', 'must supply a valid name .');
}
Return 'Hello! :, '. $ Name;
}

$ Http_raw_post_data = isset ($ http_raw_post_data )? $ Http_raw_post_data :'';

$ Server-> service ($ http_raw_post_data );
?>
After writing the server, you have to test it first. Visit the PHP page to see the following page:

Click "WSDL" to view the XML message defined in the WSDL"[Url] http://testweb.dev.php/testwebservice/testwebservice.php#/url]"/>[Url] URLs

Now I am writing a Java program to call WebService.
Example:

Package test;

Import javax. xml. rpc. serviceexception;

Import org. Apache. axis. Client. call;
Import org. Apache. axis. Client. Service;

Public class testwebservice {

Public static void main (string [] ARGs) throws exception {
// String endpoint = "http: // localhost: 8086/testwebservice/services/testwebservice ";
String endpoint = "http://testweb.dev.php/testWebService/testWebService.php"; // This section is the address just above
Service = new service ();
Call call = (CALL) service. createcall ();
Call. settargetendpointaddress (New java.net. URL (endpoint ));
Call. setoperationname ("hello ");

String Param = new string ("Chinese". getbytes (), "ISO-8859-1"); // if this section is not added, Chinese parameters will be garbled
String S = (string) Call. Invoke (new object [] {Param });
S = new string (S. getbytes ("ISO-8859-1"), "GBK"); // if there is no conversion encoding, Chinese characters will also be garbled
System. Out. println (s );
}

}

Nusoap. php defines the encoding var $ soap_defencoding = 'iso-8859-1 '. Therefore, I tried this encoding without generating Chinese garbled characters.

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.