Monday, April 20, 2009

Base64Binary-webservice consuming through PHP

This is an example for how to consume the asp.net webservice method which will return the bytes of array as datatype.

How it will be consumed in PHP client is slightly different one.The returned xsd schema data type will be not as byte as we had in ASP.NET service rather encoded base64 string.

So we need to decode the base64 string as plain text.After we can write back to stream or screen.

ASP.NET webservice code for Base64Binary type
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service ()
{


}

[WebMethod]
public byte[] ShowFileContent()
{

FileStream fs = File.OpenRead(Server.MapPath("Resume.doc"));
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
return b;
}

}

PHP Client:
using nusoap webservice class.really its very handy.so my code length shrink about to 4 or 5 lines.


require_once('lib/nusoap.php');
$client = new soapclient('http://localhost/website2/Service.asmx?WSDL','wsdl');
$result = $client->call(ShowFileContent);
$r=array($result['ShowFileContentResult']);
print_r(base64_decode($r[0]),$handle);

//here you can write into file also.

?>