Blog

Code, thoughts and all other sorts of rambling

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

My 2013 goals didn’t go so well. We just have to admit failure and prepare new goals, or repeat the one we really need to! And set time for them!

Read more →

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 →

PHP snippet to Watermark Images Automatically.

This snippet brings a one line .htaccess, if you choose to use it, any call to a image on your website, gets automatically watermarked, stopping anyone from downloading the image without the watermark.

You can also center the watermark in the following ‘variable name‘ positions:

  • TOP_LEFT , TOP_CENTER, TOP_RIGHT
  • CENTER_LEFT, CENTER, CENTER_RIGHT
  • BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT

You can either change the default settings in the .php file, or if you invoke the script direct directly you can pass parameters either in .htaccess or in a html page, as you can see in the demo.

The scripts uses two merging type functions (imagecopy and imagecopymerge).
In case you already have a .PNG or .GIF with the opacity set, you can set the script to just overlay it, otherwise if you use a JPEG or any other file without transparency set, you can choose the opacity and the script will merge it accordingly.

You can find the code over at github. You can find a Demo here.

Enjoy.

PHP class to calculate a Team based ELO Rating System for 5 vs 5.

This is heavily inspired on:

  • Heroes of Newerth
  • League of Legends
  • DotA 2

The use of this class is quite simple.
You can find the code over at github which has a usage example. You can find a demo here.

But simply, this takes two team arrays, with a mandatory elo column.
The class will then calculate both team averages, win rate probabilitys.
And when you execute the calculate function it will return the same two arrays with two new columns, containing the ‘gain’ and ‘loss’ points for each case.
You can use them as you wish.

Enjoy.

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

Hello everyone,

Welcome to my brand new Portfolio and DevBlog.
I hope you enjoy the look and the way things are organized now.

The last version no longer excited me to contribute, and since I wanted to contribute more, besides just having a gallery and share stuff in my blog, I decided to bring this version to life.

Read more →