Categories Archives

You are currently viewing all posts published under Code Snippets.

Hello,

From some of my posts you’d figure I had a big project, where many problems occurred between SOAP PHP and other services..yeah it happened.

I had to implement connections with various security protocols, the other day at StackOverflow I noticed someone having problem with this, and most of the posts have no responses at all.

This specific post will show you to establish a connection to a SOAP WebService that is secured with WSSE, the specific doc is Web Services Security Username Token Profile: http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd

In order to structure it clearly, and make sure PHP doens’t f*ck while generating the XML, we create various classes.

First we need a Class to store our username and password.

Then we need a class to store our Username Token related to this service.

Now instead of extending the SoapClient, which made errors happened in other places of communication with various other SOAP APIs.

I just make client inside another class, with the security options prepared and then return it to be used elsewhere in the code.

I have trace, exceptions and connection_timeout activated which are all optional.

After all those 3 classes are created, I can just access my prepared client like this:

Obvious as it seems, you need to indicate your user, pw, wsdl and gate (optional) to connect.

Afterwards you use it just as normal, example:

Hope this comes in handy for some of you..
Happy coding!

Hello guys,

Do you use PHP? Do you use SOAP (you better :p) default extension?
This post is to be applied in that case and in that case only.
I don’t like NuSOAP and I think it has no use anymore.

This is a pretty neat ‘hack’. After sending some data to a webservice, say you need to save the XML that is easy.. PHP gives you your request in XML.

But say you need to save the payload as it is, just to re-send it later for some reason.
The payload is the final PHP Soap Object you constructed. (Works in arrays too as the PHP SOAP Object needs to be converted to array to be sent anyway).

You may say, well I will use serialize, and store the serialization of the object, however PHP serialize does NOT work with PHP SOAP Objects.
It doesn’t recognize the classes and the objects get malformed.

This is a trouble you only experience if you are really trying to rebuild a SOAP Object and not a simple array, but trust me there are cases in which you need SOAP Objects and not just arrays to enforce complicated hierarchy of namespaces.

You move on to json_encode/decode? Then it just won’t work as all the objects will be re-constructed into Array’s..

So what you actually need is a re-constructive  function that applies the correct decode transformation to a previous json_encode of the entire Payload SOAP Object.

So after you apply json_decode and you have a extensive Array. You apply this “””beautiful””” (read neat hack) function I created to reconstruct it accordingly.

You may want to add some switch’s in case you used any different type besides XSD_STRING or just SOAP_ENC_OBJECT when constructing the parameters.
I don’t use any other type seeing as it doesn’t matter when it gets transformed to XML.

Here’s the neat function:

Enjoy. Share!

Today again I’m here because there’s a special Modulus Check Digit of the norm ISO/IEC 7064, the Mod 11,2, that I wasn’t able to find the code for in PHP.

To help you all I decided to build a version of it. Mod 11 2 isn’t a very known modulus because it isn’t used very often and perhaps that’s why there isn’t much information around.

For example the Mod 97,10 is used to calculate the check digit of an IBAN (International Bank Account Number) and so you can find it in many places.

The Mod 11-2 is used in medical related services, that’s where you will find it most of the times.

Code can also be found in the gist here and in the box below.

Share, enjoy.

 

So you found yourself looking for one of the following keywords ‘PHP SOAP Repeated Element Name‘ or ‘SOAP Structure Repeated Name‘.

Then look no further, I’m going to show you how to properly address this problem.
PHP SoapClient is not the most complete library out there, it’s actually rather poor at times, if you have the time to test and implement, a better, faster, more complete library to replace PHP SoapClient, then google this: WSO2 Web Services Framework for PHP (WSO2 WSF/PHP), it’s a paid-turned-open source project.

In reference to our problem, you may have a structure like this:

And whether you are building the response using Objects or Arrays, the answer is pretty much the same.

In case you are working with a complex WSDL Specification you are probably using Objects, for perfect control over the namespaces.

The Solution
The simplest solution relies on a pretty little thing called, ArrayObject, implemented in PHP 5, which can be used whether you are building an Array or an Object.

If you are a building simple response without need for namespace declarations, you can just put the value instead of using SoapVar’s.

The example above will output the XML repeated element example above when sent via SOAP.

Read more →

For those who use and like gettext for localization.

This is a snippet to have a simpler and more powerful way to access it over PHP default.

It automatically uses sprintf when needed, and you don’t have to write a big ass line for a simple plural translation.

Code is also in the Gist