php:获取TNT澳大利亚所有tnt运输方式的实时运费

时间:2015-07-13 19:07:46

标签: php xml api curl shipping

我想获得TNT澳大利亚所有运输方式的原产地和送货地址的实时费率。

喜欢 Road Express,Overnight Express,隔夜PAYU Satchel,9:00 Express,10:00 Express等

我正在使用以下代码。

function sendToTNTServer( $Xml ) {

$postdata = http_build_query(
                   array(
                     //For Future reference
                     //the xml_in= ( the = ) is appended
                     //Automatically by PHP
                    'xml_in' => $Xml 
                   )
        );

$opts = array('http' =>
            array(
               'method'  => 'POST',
               'header'  => 'Content-type: application/x-www-form-urlencoded',
               'content' => $postdata
             )
         );

$context  = stream_context_create( $opts );
$output = file_get_contents( 
       'http://www.tntexpress.com.au/expressconnect/pricing/getprice', 
       false, 
       $context 
     );

     return $output;
}

$XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
              <PRICEREQUEST> 
                   <LOGIN> 
                       <COMPANY>username</COMPANY> 
                       <PASSWORD>password</PASSWORD> 
                       <APPID>PC</APPID> 
                   </LOGIN> 
                   <PRICECHECK> 
                       <RATEID>rate1</RATEID> 
                       <ORIGINCOUNTRY>AU</ORIGINCOUNTRY> 
                       <ORIGINTOWNNAME>Atherstone</ORIGINTOWNNAME> 
                       <ORIGINPOSTCODE>2217</ORIGINPOSTCODE> 
                       <ORIGINTOWNGROUP/> 
                       <DESTCOUNTRY>AU</DESTCOUNTRY> 
                       <DESTTOWNNAME>Alicante</DESTTOWNNAME> 
                       <DESTPOSTCODE>6009</DESTPOSTCODE> 
                       <DESTTOWNGROUP/> 
                       <CONTYPE>N</CONTYPE> 
                       <CURRENCY>AUD</CURRENCY> 
                       <WEIGHT>18</WEIGHT> 
                       <VOLUME>1</VOLUME> 
                       <ACCOUNT/> 
                       <ITEMS>1</ITEMS> 
                 </PRICECHECK> 
            </PRICEREQUEST>";

$returnXml = sendToTNTServer( $XmlString );
echo $returnXml;

但是给我一些信息,例如登录详细信息无效。

我们的TNT帐户是在http://www.tntexpress.com.au/链接上创建的。

我使用php作为服务器端语言。

2 个答案:

答案 0 :(得分:3)

经过多次与TNT的讨论和讨论后,我得到了答案。

<?php
 /**
 *  Submit XML to the TNT
 *  server via a Stream instead
 *  of cURL. 
 *
 *  @Returns String (XML)
**/

function sendToTNTServer( $Xml ) {

$username = username starting with CIT000

$password = ******

$senderAccount = *******


$postdata = http_build_query(
                   array(
                     'Username' => $username,
                     'Password' => $password,
                    'XMLRequest' => $Xml 
                   )
        );

$opts = array('http' =>
            array(
               'method'  => 'POST',
               'header'  => 'Content-type: application/x-www-form-urlencoded',
               'content' => $postdata
             )
         );

$context  = stream_context_create( $opts );
$output = file_get_contents( 
       'https://www.tntexpress.com.au/Rtt/inputRequest.asp', 
       false, 
       $context 
     );

     return $output;
}

$XmlString = "<?xml version='1.0'?>
<enquiry xmlns='http://www.tntexpress.com.au'>
<ratedTransitTimeEnquiry>
<cutOffTimeEnquiry>
  <collectionAddress>
    <suburb>Sydney</suburb>
    <postCode>2000</postCode>
    <state>NSW</state>
  </collectionAddress>
  <deliveryAddress>
    <suburb>Melbourne</suburb>
    <postCode>3000</postCode>
    <state>VIC</state>
  </deliveryAddress>
  <shippingDate>2007-11-05</shippingDate>
  <userCurrentLocalDateTime>
    2007-11-05T10:00:00
  </userCurrentLocalDateTime>
  <dangerousGoods>
    <dangerous>false</dangerous>
  </dangerousGoods>
  <packageLines packageType='D'>
    <packageLine>
      <numberOfPackages>1</numberOfPackages>
      <dimensions unit='cm'>
        <length>20</length>
        <width>20</width>
        <height>20</height>
      </dimensions>
      <weight unit='kg'>
        <weight>1</weight>
      </weight>
    </packageLine>
  </packageLines>
</cutOffTimeEnquiry>
<termsOfPayment>
  <senderAccount>$senderAccount</senderAccount>
  <payer>S</payer>
</termsOfPayment>
</ratedTransitTimeEnquiry>
</enquiry>";

$returnXml = sendToTNTServer( $XmlString );
echo $returnXml;

?>

答案 1 :(得分:0)

<?php
function getXmlContent(){
    return '<?xml version="1.0" encoding="UTF-8"?>
    <priceRequest>
        <appId>PC</appId>
        <appVersion>3.0</appVersion>
        <priceCheck>
        <rateId>rate2</rateId>
        <sender>
            <country>GB</country>
            <town>Atherstone</town>
            <postcode>CV9 2RY</postcode>
        </sender>
        <delivery>
            <country>ES</country>
            <town>Alicante</town>
            <postcode>03006</postcode>
        </delivery>
        <collectionDateTime>' . date("Y-m-d")/*"2013-03-11T15:01:00"*/ .    '</collectionDateTime>
        <product>
           <type>N</type>
        </product>
        <currency>GBP</currency>
        <priceBreakDown>true</priceBreakDown>
        <consignmentDetails>
            <totalWeight>1.25</totalWeight>
            <totalVolume>0.1</totalVolume>
            <totalNumberOfPieces>1</totalNumberOfPieces>
        </consignmentDetails>
        </priceCheck>
    </priceRequest>';
}
function buildHttpPostData(){
    $post = http_build_query(array('xml_in' => getXmlContent()));

    return $post;
}

$url = 'https://express.tnt.com/expressconnect/pricing/getprice';

$headers[] = "Content-type: application/x-www-form-urlencoded";
$headers[] = "Authorization: Basic " . base64_encode('user:password');

$context = stream_context_create(array(
    'http' => array(
        'header' => $headers,
        'method' => 'POST',
        'content' => getXmlContent()
    )
));

$output = file_get_contents($url, false, $context);

print_r($output);