PHP向服务器发布XML请求

PHP向服务器发布XML请求

本文介绍了PHP向服务器发布XML请求(TNT Express Connect定价模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试向TNT Express连接定价模块服务发出http发布请求:

I try to make http post request to TNT Express connect pricing module service:

$xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.PHP_EOL;
$xml .= '<PRICEREQUEST>'.PHP_EOL;
$xml .='<LOGIN>'.PHP_EOL;
$xml .= '<COMPANY>xxxxx</COMPANY>'.PHP_EOL;
$xml .= '<PASSWORD>xxxxx</PASSWORD>'.PHP_EOL;
$xml .= '<APPID></APPID>'.PHP_EOL;
$xml .= '</LOGIN>'.PHP_EOL;
$xml .= '<DATASETS>'.PHP_EOL;
$xml .='<COUNTRY>1.0</COUNTRY>'.PHP_EOL;
$xml .= '<CURRENCY>1.0</CURRENCY>'.PHP_EOL;
$xml .= '<POSTCODEMASK>1.0</POSTCODEMASK>'.PHP_EOL;
$xml .='<TOWNGROUP>1.0</TOWNGROUP>'.PHP_EOL;
$xml .='<SERVICE>1.0</SERVICE>'.PHP_EOL;
$xml .='<OPTION>1.0</OPTION>'.PHP_EOL;
$xml .='</DATASETS>'.PHP_EOL;
$xml .='<PRICECHECK>'.PHP_EOL;
$xml .='<RATEID>rate1</RATEID>'.PHP_EOL;
$xml .='<ORIGINCOUNTRY>GB</ORIGINCOUNTRY>'.PHP_EOL;
$xml .='<ORIGINTOWNNAME></ORIGINTOWNNAME>'.PHP_EOL;
$xml .='<ORIGINPOSTCODE></ORIGINPOSTCODE>'.PHP_EOL;
$xml .='<ORIGINTOWNGROUP></ORIGINTOWNGROUP>'.PHP_EOL;
$xml .='<DESTCOUNTRY>AU</DESTCOUNTRY>'.PHP_EOL;
$xml .='<DESTTOWNNAME></DESTTOWNNAME>'.PHP_EOL;
$xml .='<DESTPOSTCODE></DESTPOSTCODE>'.PHP_EOL;
$xml .='<DESTTOWNGROUP></DESTTOWNGROUP>'.PHP_EOL;
$xml .='<CONTYPE>D</CONTYPE>'.PHP_EOL;
$xml .='<CURRENCY>GBP</CURRENCY>'.PHP_EOL;
$xml .='<WEIGHT>0.2</WEIGHT>'.PHP_EOL;
$xml .= '<VOLUME>0</VOLUME>'.PHP_EOL;
$xml .= '<ITEMS>1</ITEMS>'.PHP_EOL;
$xml .='</PRICECHECK>'.PHP_EOL;
$xml .='</PRICEREQUEST>';

$url = 'iconnection.tnt.com';
$lenght = strlen($xml)+7;
$headers = array(
                "POST PriceGate.asp HTTP/1.0",
                "Accept:*/*",
                "User-Agent: PriceGate_socket/1.0",
                "Content-type: application/x-www-form-urlencoded",
                "Content-length: $lenght",
                );



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_PORT, 81);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result =  curl_exec($ch);

 if (empty($result)) {
   // some kind of an error happened
    die(curl_error($ch));
      curl_close($ch); // close cURL handler
   } else {
      $info = curl_getinfo($ch);
      curl_close($ch); // close cURL handler

      if (empty($info['http_code'])) {
              die("No HTTP code was returned");
      } else {
//

       // echo results
          echo "The server responded: \n";
          echo $info['http_code'];
      }
   }


   echo '<pre>';
   print_r ($result);
   echo '</pre>';

但是得到回应:无法连接到主机

But got response: couldn't connect to host

我做错了什么?也许还有其他解决方案可以交互tnt服务或获取一些调试信息?非常感谢

What i doing wrong?Mayby there is some another solution to interact tnt services or get some debug information?Many thanks

更新

现在的响应是:

The server responded: 405

HTTP/1.1 405 Method Not Allowed
Allow: OPTIONS, TRACE, GET, HEAD
Content-Length: 1564
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 11 Sep 2013 10:12:01 GMT

更新2

这是我的新PHP代码: http://pastebin.com/a4s4yLLu

Here is my new php code: http://pastebin.com/a4s4yLLu

现在我得到了答复:Fatal error: Curl failed with error #7: couldn't connect to host in /home/client/public_html/tnt/test3.php on line 104

推荐答案

虽然这可能要晚30天..我实际上已经被分配了相同的项目.

Whilst this may be 30 days late.. I've actually been assigned the same project.. x) But took me a while to tinker around with it..

<?php
    /**
     *  Submit XML to the TNT
     *  server via a Stream instead
     *  of cURL.
     *
     *  @Returns String (XML)
    **/
    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(
           'https://express.tnt.com/expressconnect/pricing/getprice',
           false,
           $context
         );

         return $output;
    }

    $XmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
                  <PRICEREQUEST>
                       <LOGIN>
                           <COMPANY>YOUR USERID</COMPANY>
                           <PASSWORD>YOUR PASSWORD</PASSWORD>
                           <APPID>PC</APPID>
                       </LOGIN>
                       <PRICECHECK>
                           <RATEID>rate1</RATEID>
                           <ORIGINCOUNTRY>GB</ORIGINCOUNTRY>
                           <ORIGINTOWNNAME>Atherstone</ORIGINTOWNNAME>
                           <ORIGINPOSTCODE>CV9 2RY</ORIGINPOSTCODE>
                           <ORIGINTOWNGROUP/>
                           <DESTCOUNTRY>ES</DESTCOUNTRY>
                           <DESTTOWNNAME>Alicante</DESTTOWNNAME>
                           <DESTPOSTCODE>03006</DESTPOSTCODE>
                           <DESTTOWNGROUP/>
                           <CONTYPE>N</CONTYPE>
                           <CURRENCY>GBP</CURRENCY>
                           <WEIGHT>0.2</WEIGHT>
                           <VOLUME>0.1</VOLUME>
                           <ACCOUNT/>
                           <ITEMS>1</ITEMS>
                     </PRICECHECK>
                </PRICEREQUEST>";

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

这篇关于PHP向服务器发布XML请求(TNT Express Connect定价模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:21