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,

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


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

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

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



client side:

$requestPayloadString = <<<XML
<ns1:echoString xmlns:ns1=""><text>Hello World!</text></ns1:echoString>

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: