Being Binary in SOAP

Well, it might not be the best way to do it, but here’s a way I found to send binary data via PHP’s SOAP extension from one place to another:


function recieveFile($data){
	if($fp){ fwrite($fp,$data); fclose($fp); }

	return strlen($data).' written';
$input	= file_get_contents("php://input");
$server = new SoapServer(
	'http://[my url here]/binary.wsdl',

and the Client:

ini_set("soap.wsdl_cache_enabled", "0");

//send binary data
$client=new SoapClient(
	'http://[my url here]/binary.wsdl',


It’s pretty basic – the client just reads in the file’s information, runs it through a call to base64_encode before it’s sent off to the server. Once it’s there a mirror call to base64_decode gets the content back to normal and it’s written out to the “img_out.gif” file on the remote server.

It’s probably not the best way to do it, but its one that I’ve found that works quickly and easily. I tried to just send the binary data without base64 encoding it and it didn’t want to cooperate (it was only getting the first chunk of data). I don’t know how well it’ll perform with larger files, though – we shall see.



  1. I do something similar in a productive environment for some time now. If you have very large files you might want to split the base64-encoded data and transfer them as chunks.

    compressing before base64-encoding might also be a good idea, but that depends on the nature of the data.


Leave a Reply to Lars Pohlmann Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s