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:

Server:

function recieveFile($data){
$data=base64_decode($data);
$path='/my/file/path/img_out.gif';
$fp=fopen($path,'w+');
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',array('encoding'=>'ISO-8859-1'));
$server->addFunction('recieveFile');
$server->handle();

and the Client:

ini_set("soap.wsdl_cache_enabled", "0");
//send binary data
$client=new SoapClient('http://[my url here]/binary.wsdl',array('encoding'=>'ISO-8859-1'));
$data=file_get_contents('/my/file/path/img_to_send.gif');
$ret=$client->recieveFile(base64_encode($data));
print_r($ret);
[/code]
 
It's pretty basic - the client just reads in the file's information, runs it through a call to <a href="http://php.net/base64_encode" onclick="javascript:pageTracker._trackPageview ('/outbound/php.net');">base64_encode</a> before it's sent off to the server. Once it's there a mirror call to <a href="http://php.net/base64_decode" onclick="javascript:pageTracker._trackPageview ('/outbound/php.net');">base64_decode</a> 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.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)