Thursday, January 28, 2010

Accessing Webservices from PHP

Normally, most of the current windows products provides webservice for many of its functionalities. Particularly the sharepoint 2007 server, has lot of webservice that external applications can use it.

Again, accessing those webservices through .NET is pretty easy with its built in function. But, to access it through languages like PHP, requires some analysis, particularly for those who are doing it for first time.
The nusoap.php is a populat choice to access the webservice via Php.

It can be downloaded from the following sourceforge URL.

href="http://sourceforge.net/project/showfiles.php?group_id=57663

Here is the code snippet to access it

require_once( '/nusoap.php');

$client = new soapclient('http://ctsgvcchannel1/_vti_bin/SearchWebService.asmx',false,$proxyhost, $proxyport, $proxyusername, $proxypassword);

$client->setcredentials($username,$password);

$err = $client->getError();

if ($err) {

echo '

Constructor error

' . $err . '
';

}

// Doc/lit parameters get wrapped

$param = array('strKeyWord' => 'java','pageLength'=>(int)'5');

$result = $client->call('SimpleSearch', $param, '', 'http://tempuri.org/SimpleSearch', false, true);

// Check for a fault

if ($client->fault) {

echo '

Fault

';

print_r($result);

echo '
';


} else {

// Check for errors

$err = $client->getError();

if ($err) {

// Display the error

echo '

Error

' . $err . '
';


} else {

// Display the result

echo '

Result

';

print_r($result);

echo '
';


}

}

echo '

Request

' . htmlspecialchars($client->request, ENT_QUOTES) . '
';


echo '

Response

' . htmlspecialchars($client->response, ENT_QUOTES) . '
';

No comments: