Http Basic Authentication in WSF/PHP

Monday, June 9, 2008

Normally in Web Services stacks authentication is done using user name token comes with WS-Security specifications. But if you are more interacting with legacy systems, you may have come across situations where you have to use http authentication.

Here is how you are going to do it with WSF/PHP. I will take simple echo sample to demonstrate this,

server side:
You have to configure apache to do the authentication, so the php and wsf/php applications which are running behind the apache server also get authenticated. The following guide will help you to do the configuration very easily,
http://apache.active-venture.com/auth-basic.html

Your server side code is same as the code for a regular service


<?php

function echoFunction($inMessage) {
$outMessage = new WSMessage($inMessage->str);
return $outMessage;
}

$operations = array("echoString" => "echoFunction");

$service = new WSService(array("operations" => $operations));

$service->reply($requestPayloadString);

?>



client side:


<?
$requestPayloadString = <<<XML
<ns1:echoString xmlns:ns1="http://php.axis2.org/samples"><text>Hello World!</text></ns1:echoString>
XML;

try {

$client = new WSClient(array( "to" => "http://localhost/samples/echo_service.php",
"httpAuthUsername" => "xxx",
"httpAuthPassword" => "x@x",
"httpAuthType" => "Basic",
));

$responseMessage = $client->request( $requestPayloadString );

printf("Response = %s
", htmlspecialchars($responseMessage->str));

} catch (Exception $e) {

if ($e instanceof WSFault) {
printf("Soap Fault: %s\n", $e->Reason);
} else {
printf("Message = %s\n",$e->getMessage());
}

}
?>




You will fill the fields specific to http authentication. And here you have web service client and service which uses http basic authentication.

No comments: