PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Developing Web Services Using PHP
Pages: 1, 2, 3, 4

Create a PHP script, xmlrpc-webservice.php, in the C:/Apache2/htdocs directory. In the PHP script, define a function, hello_func. Any function that is invoked by a client is required to take three parameters: the first parameter is the name of the XML-RPC method invoked. The second parameter is an array containing the parameters sent by the client. The third parameter is the application data sent in the user_data parameter of the xmlrpc_server_call_method() function. In the hello_func function, retrieve the first parameter, which is a name sent by the client, and output a Hello message.



function hello_func($method_name, $params, $app_data)
{
$name = $params[0];
return "Hello $name.";
}

Create an XML-RPC server using the xmlrpc_server_create() method.

$xmlrpc_server=xmlrpc_server_create();

If a server does not get created the xmlrpc_server_create method returns FALSE. Register the hello_func function with the server using the xmlrpc_server_register_method method. The first argument to the xmlrpc_server_register_method method is the XML-RPC server resource. The second argument is name of the method that is provided by the web service, which is the <methodName> element value in a XML-RPC request. The third argument is the PHP function to be registered with the server.

$registered=xmlrpc_server_register_method 
($xmlrpc_server, "hello", "hello_func" );

If the PHP function gets registered, the xmlrpc_server_register_method returns TRUE.

Creating an XML-RPC Client

Next, I shall create a XML-RPC client to send a request to the XML-RPC server. First, specify the XML string to be sent in the request.

$request_xml = <<< END
<?xml version="1.0"?>
<methodCall>
    <methodName>hello</methodName>
      <params>
        <param>
          <value>
            <string>Deepak</string>
          </value>
        </param>
      </params>
<methodCall>
END;

To escape XML in PHP, <<<END ….END; is used. An XML document demarker other END may also be used. The methodCall element specifies the web service method that is to be invoked. Invoke the web service method using the xmlrpc_server_call_method function. The first argument to the xmlrpc_server_call_method function is the server resource. The second argument is the string containing the XML-RPC request. The third argument is the application data that is sent to the third parameter of the method handler function.

$response=xmlrpc_server_call_method( $xmlrpc_server, 
$request_xml, '', array(output_type => "xml"));

Output the XML-RPC response from the server.

print $response;

The PHP script, xmlrpc-webservice.php, is listed below.

<?php
function hello_func($method_name, $params, $app_data)
{
$name = $params[0];
return "Hello $name.";
}

$xmlrpc_server=xmlrpc_server_create();
$registered=xmlrpc_server_register_method 
($xmlrpc_server, "hello", "hello_func" );

$request_xml = <<< END
<?xml version="1.0"?>
<methodCall>
    <methodName>hello</methodName>
      <params>
        <param>
          <value>
            <string>Deepak</string>
          </value>
        </param>
      </params>
</methodCall>
END;
$response=xmlrpc_server_call_method( $xmlrpc_server, 
$request_xml, '', array(output_type => "xml"));
print $response;
?>

Copy the xmlrpc-webservice.php script to the C:/Apache2/htdocs directory. Invoke the PHP script with the URL http://localhost/xmlrpc-webservice.php. The response from the server gets output to the browser as shown in Figure 2.

Response From XML-RPC Web Service
Figure 2. Response from XML-RPC web service

To demonstrate an error in the request, returned as a <fault> element in the response XML, make the request XML not valid XML. For example replace </methodCall> with <methodCall> in the $request_xml. Invoke the xmlrpc-webservice.php. The response from the server is returned as a <fault> element that consists of a struct element value, which consists of faultCode and faultString members, as shown in Figure 3.

Response As fault Element
Figure 3. Response as fault element

References

For more information, see W3C's WSDL page and SOAP Primer.

Deepak Vohra is a NuBean consultant and a web developer.


Return to ONLamp.com.


Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: