Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soap - base64binary data in PHP

I have a SOAP client in PHP that makes calls to a WSDL service. One of the functions returns a base64binary data. I've been trying to decode it without any luck.

base64_decode($encoded_base64data) will not work. I tried using base_convert() and mv_convert_encoding() with various parameters, but could not get a proper result.

The encoded result data starts with:

��`I�%&/m�{J�J��t��`$ؐ@�������iG#)�*��eVe]f@�흼��{����{����;�N'���?\fdl��J�ɞ!���?~|?"

(the data is much longer, this is just a small portion of the string)

Any idea how it could be done?

Thanks

EDIT

I've extended the SoapClient with a new __doRequest() method to check that the received data is a proper base64 string. I got a proper base64 encoded string, and the result shown above is the decoded response.

Anyhow, the string was decoded automatically by the SoapClient from base64 to binary (as @hakre suggested), so I only have to deal with the binary response.

Now what I need is to decode the binary string into something that would look like a readable format. The final response should contain Georgian output, so I'm trying to figure out the original encoding (but that's a different question).

like image 804
galchen Avatar asked Jun 03 '13 09:06

galchen


2 Answers

From base64Binary (XML Schema Part 2: Datatypes 3.2.16):

[Definition:] base64Binary represents Base64-encoded arbitrary binary data. The value space of base64Binary is the set of finite-length sequences of binary octets. For base64Binary data the entire binary stream is encoded using the Base64 Content-Transfer-Encoding defined in Section 6.8 of [RFC 2045].

You then comment:

When a WSDL has xsd:base64binary am I supposed to get a base64 response or a binary response or a base64 encoded string?

You are supposed to get a base64 encoded string. That base64 encoded string represents the binary data. If you know the XML specification, this might be more obvious because you can not pass binary information with XML, you can only pass information that fit's into XML's character-range. And that range excludes characters that are part of binary data, especially control characters and the higher pane if you divide the binary octet into a lower and higher one. See Characters (Extensible Markup Language (XML) 1.0 (Fifth Edition) 2.2) which shows that XML is about characters, not binary data. And which also shows which binary data those characters do form (and which they can not form).

Therefore the base64Binary encoding has been defined as one way to transport binary data within an XML document. So what you've got inside the raw XML response to your SOAP request is never binary but base64 encoded binary data.

Take care that your SOAP-client might already deal with this encoding and provide the data decoded.

like image 157
hakre Avatar answered Oct 20 '22 20:10

hakre


Although previous answer is absolutely correct but I feel this may be helpful to get quick solution.

When we check in response xml then we see base64 encoded data and we try to decode it in our code to get the real data but it is not required.

Remove base64_decode.

Because SOAP client internally decode itself.

like image 9
Amit Garg Avatar answered Oct 20 '22 21:10

Amit Garg