selimppc

TNT Express and Integration in PHP

May 4th, 2016
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. make a class file ::
  2. ====================
  3. <?php
  4. /**
  5.  * Created by PhpStorm.
  6.  * User: selimreza
  7.  * Date: 5/3/16
  8.  * Time: 10:29 PM
  9.  */
  10. namespace App\Helpers;
  11. class TntExpress
  12. {
  13.  
  14.     /**
  15.      *  Submit XML to the TNT
  16.      *  server via a Stream instead
  17.      *  of cURL.
  18.      *
  19.      *  @Returns String (XML)
  20.      **/
  21.     public static function sendToTNTServer( $Xml ) {
  22.         $postdata = http_build_query(
  23.             array(
  24.                 //For Future reference
  25.                 //the xml_in= ( the = ) is appended
  26.                 //Automatically by PHP
  27.                 'xml_in' => $Xml
  28.             )
  29.         );
  30.         $opts = array('http' =>
  31.             array(
  32.                 'method'  => 'POST',
  33.                 'header'  => 'Content-type: application/x-www-form-urlencoded',
  34.                 'content' => $postdata
  35.             )
  36.         );
  37.         $context  = stream_context_create( $opts );
  38.         $output = file_get_contents(
  39.             'https://express.tnt.com/expressconnect/pricing/getprice',
  40.             false,
  41.             $context
  42.         );
  43.         return $output;
  44.     }
  45. }
  46.  
  47.  
  48. call the method in controller::
  49. ===============================
  50. public function xml_data(){
  51.         $XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  52.                  <PRICEREQUEST>
  53.                       <LOGIN>
  54.                           <COMPANY>Asim</COMPANY>
  55.                           <PASSWORD>tnt12345</PASSWORD>
  56.                           <APPID>IN</APPID>
  57.                       </LOGIN>
  58.                       <PRICECHECK>
  59.                           <RATEID>rate1</RATEID>
  60.                           <ORIGINCOUNTRY>GB</ORIGINCOUNTRY>
  61.                           <ORIGINTOWNNAME>Atherstone</ORIGINTOWNNAME>
  62.                           <ORIGINPOSTCODE>CV9 2RY</ORIGINPOSTCODE>
  63.                           <ORIGINTOWNGROUP/>
  64.                           <DESTCOUNTRY>ES</DESTCOUNTRY>
  65.                           <DESTTOWNNAME>Alicante</DESTTOWNNAME>
  66.                           <DESTPOSTCODE>03006</DESTPOSTCODE>
  67.                           <DESTTOWNGROUP/>
  68.                           <CONTYPE>N</CONTYPE>
  69.                           <CURRENCY>GBP</CURRENCY>
  70.                           <WEIGHT>0.2</WEIGHT>
  71.                           <VOLUME>0.1</VOLUME>
  72.                           <ACCOUNT/>
  73.                           <ITEMS>1</ITEMS>
  74.                     </PRICECHECK>
  75.                </PRICEREQUEST>";
  76.         $tnt = TntExpress::sendToTNTServer($XmlString);
  77.         print_r($tnt);
  78.  
  79.         echo "OK";
  80.     }
  81.  
  82.  
  83. OutPut::
  84. ========
  85. "login details not recognised"
Advertisement
Add Comment
Please, Sign In to add comment