Have visited: times          Discuss         Manager

                                           SOAP Client and Server for PHP

        This is a PHP implementation of the SOAP submission to W3C.SOAP is a lightweight protocol for exchange of information in a decentralized, distributed environment. It is an XML based protocol that consists of three parts: an envelope that defines a framework for describing what is in a message and how to process it, a set of encoding rules for expressing instances of application-defined datatypes, and a convention for representing remote procedure calls and responses.

     Download soap_php.zip here.

Click here to download the new phpsoap client to call phpsoap service example.

  How to install:

         Ensure that you have compiled PHP with --with-xml option.

         We assume that you have downloaded this distribution of SOAP Client and Server for PHP and place            soap_client.inc into   your PHP Include Directory,then you are ready to use SOAP Client!

    If you want to use your SOAP Server for PHP,you should place rpcrouter.php in any of your web directories

other than above step.

  How to use:

         Here is a sample code that use PHP invoke JAVA class:

         CLient Side Program Written in PHP:

         <?

          require "soap_client.inc";   //First step in our program,don't forget!

//Build the call.
$send_int=2001;
$send_string="happy!";
$send_arr=array("2001","happy" );
$soap_msg=new soapmsg( 'get_array',        //Invoke Method Name
                       'urn:soap_test',                       //Invoke Target Object Id
                        array(                                          //Parametres List Below
                        new soapval($send_int,"int"),
                        new soapval($send_string,"string"),
                        new soapval($send_arr, "array")));

//Invoke the call.
$client=new soap_client("/soap/servlet/rpcrouter", "office.gigaideas.com.cn", 8080);

Note: /soap/servlet/rpcrouter is soap server side servlet program.

            office.gigaideas.com.cn & 8080 is server name and port.

$return=$client->send($soap_msg);
if (!$return) { die("send failed"); }
$v=$return->value();

//Check the response.
if(!$return->faultCode()) {
    $back_arr=$v->scalarval();
    print "Back array is:\n<br>";
    for($i=0;$i<sizeof($back_arr);$i++) echo $i." => ".$back_arr[$i]."\n<br>";
} else {
print "Fault: ";
print "Code: " . $return->faultCode() . " Reason '" .$return->faultString()."'<BR>";
}

?>

Server Side Program Written in JAVA:

package samples.soaptest;

import java.io.*;
import java.util.*;
import java.lang.*;

public class soap_test {

public int get_int(int x,String y,Object[] z) {
    return x;
}
public String get_string(int x,String y,Object[] z) {
    return y;
}
public Vector get_array(int x,String y,Object[] z) {
    Vector params = new Vector ();    //Turn array into Vector
    for(int i=0;i<z.length;i++) {     //Add element to Vector
        String param_type=z[i].getClass().getName();
        if(param_type=="java.lang.String")
        params.addElement((String)z[i]);
        else if(param_type=="java.lang.Integer")
        params.addElement((Integer)z[i]);
        else if(param_type=="java.lang.Double")
        params.addElement((Double)z[i]);
        else
        params.addElement((String)z[i]);
    }

    //Now we can operate this Vector

    //ToDo Something Here
    return params;      //Return this Vector
}
}

กก

 Here is a sample code that use JAVA invoke PHP function:

 Part of CLient Side Program Written in JAVA:

 // Build the call.
    Call call = new Call ();
    call.setTargetObjectURI ("functions.inc");    //Invoke Target Function Filename
    call.setMethodName ("plus");                  //Invoke Method Name
    call.setEncodingStyleURI(encodingStyleURI);  
    Vector params = new Vector ();
    params.addElement (new Parameter("params1", double.class,new Double (arg1), null));
    params.addElement (new Parameter("params2", double.class,new Double (arg2), null));
    call.setParams (params);
Note: param name should named as param+n when you use This PHP SOAP Server.
// make the call
Response resp = call.invoke ( "http://office.gigaideas.com.cn/soap_php_server/rpcrouter.php,"" );

//http://office.gigaideas.com.cn/soap_php_server/rpcrouter.php is SOAP Server side php program.

 Server Side Program Written in PHP:

<?

//filename:functions.inc

function plus($x,$y) {

    $z=$x+$y;

    return $z;

}

?>

 Declaration:

        The sample list above has been tested successfully under SOAP2.1 .

        All data types supported except object.

  Examples:

     I.soaptest.php

    II. soaptest_struct.php

 

Example to call a php soap service from php client program:

I.soaptest_php2php.php

Click here to download the new phpsoap client to call phpsoap service example.

What's include:

I. The latest version of phpsoap client: soap_client.inc

II.The latest version of phpsoap server: rpcrouter.php

III.A new service example: server.php

IV. A new client example: soaptest_php2php.php

Installation of the example:

Just extract the zip file to your the root of your web document directory. Than change the setting of new soap_client() to locate to the setting of your own machine.

That's it!!! :) You can view the soaptest_php2php.php page now.

 Any problem or suggestion please mail to:

victor@gigaideas.com.cn , or, steven@gigaideas.com.cn