TNT ExpressConnect with PHP 5.3

I had a project recently where I had to integrate TNT’s popular ExpressConnect module. This is a well developed module that has been generating Consignments over an Web API for years. Brilliant for allowing e-commerce sites to:

  • Price
  • Book
  • Track
  • Ship
  • Manifest

All from your website. I’ve recently had to integrate it along with one of our LAMP Stack websites; normally APIs in the past I’ve worked with are a straight forward API cURL request; although this had to entail a little different way of working. I never 100% understood why, although I think it’s something to do with the headers generated from cURL module which were being rejected by TNT, instead resorted to stream_get_contents to collect the $_POST response.

<br />
    /**<br />
     *  Submit XML to the TNT<br />
     *  server via a Stream instead<br />
     *  of cURL.<br />
     *<br />
     *  @Returns String (XML)<br />
    **/<br />
    function sendToTNTServer( $Xml ) {</p>
<p>       $postdata =<br />
         http_build_query(<br />
                       array(<br />
                         //For Future reference<br />
                         //the xml_in= ( the = ) is appended<br />
                         //Automatically by PHP<br />
                        'xml_in' =&gt; $Xml<br />
                       )<br />
<p>       $opts = array(<br />
                'http' =&gt; array(<br />
                   'method'  =&gt; 'POST',<br />
                   'header'  =&gt; 'Content-type: application/x-www-form-urlencoded',<br />
                   'content' =&gt; $postdata<br />
                 )<br />
<p>        $context  = stream_context_create( $opts );<br />
        $output = file_get_contents(<br />
              '',<br />
              false,<br />
              $context<br />
<p>        return $output;<br />
    }<br />