11

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).

galchen
  • 5,252
  • 3
  • 29
  • 43
  • 1
    "Will not work", "various parameters" and "proper result" are not really helpful. Show the exact input, the exact code used, the exact output and the output you expect. – CodeCaster Jun 03 '13 at 09:48
  • base64_decode($encoded_base64data) means it will not give a proper string (with expected encoding). This is true for all of my other attempts. Various parameters would be 'BASE64' and 'UTF-8' for mv_convert_encoding() and some integers for base_convert(). Neither of those gave me a well decoded response.... – galchen Jun 03 '13 at 09:51
  • "exact code used" would be base64_decode($encoded_base64data). I think the question is clear enough already. – galchen Jun 03 '13 at 09:52
  • 1
    No, the question is absolutely not clear, otherwise I wouldn't ask. Base64 is not that advanced, and `base_convert()` will most likely not contain a bug specific to your situation, so the problem is in your data. If you don't show your data and the way you verify that `data == base64_decode(base64_encode(data))`, this question can simply not be answered. – CodeCaster Jun 03 '13 at 09:55
  • @CodeCaster, that's about the only data I have to show – galchen Jun 03 '13 at 10:02
  • 3
    The string you've shown doesn't look base64 encoded to me. Base64 encoded strings tend to look kinda like this: `dGhpcyBzdHJpbmcgd2FzIGJhc2U2NCBlbmNvZGVk`. They only use 64 different characters, (a-zA-Z0-9 and two more standard specific characters) and sometimes the equals (=) character for padding – Pudge601 Jun 03 '13 at 10:22
  • I know, it's base64binary – galchen Jun 03 '13 at 10:41
  • @CodeCaster is right. What you are getting seems to be base64 decoded(for incorrect base64 string) and not base64 encoded data. Check the second answer to this http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php. The string you get is similar to the decoded result shown there. Most likely the source of the data is decoding instead of encoding it (mixed encode/decode). – user568109 Jun 05 '13 at 11:31
  • I'd assume the source (webservice) is working properly. I assumed that the result I get is binary data. When a WSDL has xsd:base64binary am I supposed to get a base64 response or a binary response or a base64 encoded string? – galchen Jun 05 '13 at 11:49
  • What kind of data is it ought to be? I mean, is it an executable or an picture? Whatever it is ought to be, it maybe already *is* decoded. Have you ever tried using it in its purpose, like saving it to a file and viewing/executing it? Please take a look at this comment on the documentation: http://www.php.net/manual/pl/soapclient.soapclient.php#98658 This guy says, that if you pass along your data (with SoapVar), you better not encode your data with base64_encode because Soap (SoapVar?) does it for you. It could probably also be true for the other way. – Honk der Hase Jun 05 '13 at 20:47
  • If you want users to tinker with your data, you must at least provide the hex-dump of your string next to the UTF-8 output you've pasted into your question. See: [How can I get a hex dump of a string in PHP?](http://stackoverflow.com/q/1057572/367456) – hakre Jun 10 '13 at 09:10
  • galchen, in my experience decoding text that has already been decoded from base64 format has output that looks similar to what you are seeing. It may be that the text in "$encoded_base64data" your are attempting to decode has been decoded already or was never in base64 format to begin with. Check the content of "$encoded_base64data" to see if it is in the format you are expecting before preforming a decode. – Rick Sarvas Jun 10 '13 at 15:24

2 Answers2

15

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 2
    The output was already decoded automatically by the SoapClient as you suggested. Didn't solve my problem yet but definitely answered my question. Thanks – galchen Jun 11 '13 at 08:46
  • 1
    Great for pointing out that SoapClient autmatically manages encoding. I was doing double encoding :/ – Marco Marsala Oct 01 '15 at 07:32
  • 1
    Thank you so much for pointing out SOAPClient automatically manages encoding. You are my hero today. – Gael Musi Feb 23 '20 at 11:40
10

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.

Amit Garg
  • 3,867
  • 1
  • 27
  • 37