Tuesday, January 15, 2008

Sending a binary image as base64 text using WSF/PHP

Today I will discuss how to send a binary image as base64 text using WSF/PHP.

$requestPayloadString = <<<XML
<ns1:upload xmlns:ns1="http://php.axis2.org/samples/mtom">
<ns1:fileName>test.jpg</ns1:fileName>
<ns1:image xmlmime:contentType="image/jpeg" xmlns:xmlmime="http://www.w3.org/2004/06/xmlmime">
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:myid1"></xop:Include>
</ns1:image>
</ns1:upload>
XML;

Note the string myid1 given after the "cid:myid1".

Next I read a binary image to a variable.

$f = file_get_contents("axis2.jpg");

Now I can create an array with $f associated to string "myid1".

$attachment = array("myid1"=>$f);

Next step is to create a WSMessage object and set this $attachment array to it.


$requestMessage = new WSMessage($requestPayloadString,
array("to" => "http://localhost/samples/mtom/mtom_service_base64.php",
"attachments" => $attachment));

Since I want to send the image as a base64 ,I will set the useMTOM property to FALSE when creating the WSClient object.

$client = new WSClient(array("useMTOM" =>FALSE));

Now all configured and to send the message.

$responseMessage = $client->request($requestMessage);

echo $responseMessage->str;

When sending the message, WSF/PHP will take out the node and put the base64 converted message in there.




No comments:

Post a Comment