【问题标题】:php : get TNT Australia live shipping rates of all tnt shipping methodsphp : 获取所有 tnt 运输方式的 TNT Australia 实时运费
【发布时间】:2015-10-02 04:41:40
【问题描述】:

我想获取 TNT Australia 的所有运输方式在发货地和收货地址之间的实时费率。

Road Express、Overnight Express、Overnight 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;

但给我的信息是登录详细信息无效。

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

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

【问题讨论】:

  • 我可以通过 cUrl 获取实时汇率吗???

标签: php xml api curl shipping


【解决方案1】:

经过与 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;

?>

【讨论】:

  • 嗨,Hiren,您能解释一下我们如何仅发送邮政编码并获得输出是 TNT 支持,还是有什么简单的方法可以实现这一点?根据邮政编码获取郊区。我主要关心的是澳大利亚
  • 这很好用。是否有任何可用的测试凭据?
  • 我可以让它工作,但你如何表达输出?
【解决方案2】:
<?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);

【讨论】:

  • 请解释您的代码,以便 OP 了解正在发生的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多