【问题标题】:USPS API for tracking shipments in PHP用于在 PHP 中跟踪货物的 USPS API
【发布时间】:2015-06-05 16:25:27
【问题描述】:

我有问题。我在 USPS 网站上注册,然后从那里获取用户并通过访问他们的 API。但是当我尝试访问跟踪时,我得到一个包含所有结果的单个文本字符串,而没有格式化 XML

我使用的脚本是这样的:

    $trackingNumber = $numtrack;
    $url = "http://production.shippingapis.com/shippingAPI.dll";
    $service = "TrackV2";
    $xml = rawurlencode("<TrackRequest USERID='MYIDACCOUNT'><TrackID ID='".$trackingNumber."'></TrackID></TrackRequest>");  
    $request = $url . "?API=" . $service . "&XML=" . $xml;
    // send the POST values to USPS
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$request);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // parameters to post

    $result = curl_exec($ch);
    curl_close($ch);

    $response = new SimpleXMLElement($result);
    print_r($result);
    $deliveryStatus = $response->TrackResponse->TrackSummary->Status;
    echo $deliveryStatus;

我得到的结果是

您的物品已于 2015 年 6 月 5 日上午 10:51 在意大利交付。海关 清关处理完成,2015 年 6 月 4 日下午 5:29,意大利海关 清关,2015 年 6 月 4 日,上午 11:20,意大利通过排序处理 设施,2015 年 6 月 4 日,上午 11:19,意大利出发,2015 年 6 月 3 日,7:43 上午,意大利米兰,2015 年 6 月 2 日下午 3:34 出发,美国迈阿密 状态已于 2015 年 6 月 2 日上午 10:05 到达,美国迈阿密已处理 通过排序设施,2015 年 6 月 1 日,晚上 8:07,ISC MIAMI FL (USPS)2015 年 6 月 1 日晚上 8:07 抵达分拣设施,ISC MIAMI FL (USPS)离开的 USPS 设施,2015 年 6 月 1 日,凌晨 2:40,佛罗里达州迈阿密 331122015 年 5 月 31 日上午 11:22 抵达 USPS Origin Facility,迈阿密, FL 33112 离开邮局,2015 年 5 月 30 日,下午 4:07,佛罗里达州博卡拉顿 33431验收,2015 年 5 月 30 日,下午 12:27,佛罗里达州博卡拉顿 33431 2015 年 5 月 29 日发送至 USPS 的装运前信息

如何获取 XML,然后推断我需要的个人信息?我跳不出来。

【问题讨论】:

    标签: php api curl usps


    【解决方案1】:

    您的浏览器将 XML 标记解释为 HTML 标记。如果您希望将 XML 回显给用户,请使用 htmlspecialchars 转义特殊字符(如 &lt;):

    echo htmlspecialchars($deliveryStatus);
    

    【讨论】:

      【解决方案2】:

      您必须构建您的 xml 响应:

      类似这样的:

      <?php
      /*
      SimpleXMLElement Object ( [TrackInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => CV034836340US ) [TrackSummary] => Your item was delivered in ITALY at 10:51 am on June 5, 2015. [TrackDetail] => Array ( [0] => Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALY [1] => Customs Clearance, June 4, 2015, 11:20 am, ITALY [2] => Processed Through Sort Facility, June 4, 2015, 11:19 am, ITALY... ) ) )
      */
      //SIMULATION FROM YOUR OBJECT
      $response = (object)array(
      'TrackInfo'=>array('ID'=>12342432),
      'TrackSummary'=>'Your item was delivered in ITALY at 10:51 am on June 5, 2015.',
      'TrackDetail' => array('Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALY','Customs Clearance, June 4, 2015, 11:20 am, ITALY','Processed Through Sort Facility, June 4, 2015, 11:19..., ITALY... ')
      );
      
      
      $string = '<?xml version="1.0" encoding="utf-8"?>
      <TrackResponse>
          <TrackInfo>'.(string)$response->TrackInfo['ID'].'</TrackInfo>
          <TrackSummary>'.(string)$response->TrackSummary.'</TrackSummary>
          <TrackDetail>';
          foreach($response->TrackDetail as $detail){
              $string .= '<detail>'.(string)$detail.'</detail>';
          }
      $string .='</TrackDetail>
      </TrackResponse>';
      
      header('Content-Type: application/xml; charset=utf-8');
      echo $string
      ?> 
      

      【讨论】:

      • 我已经设置了标题,但是我看到的结果是一个连续的字符串
      • 你回显到 $result 了吗?
      • 是的,结果和print_r($ result);一样
      • print_r $response;返回这个SimpleXMLElement Object ( [TrackInfo] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [ID] =&gt; CV034836340US ) [TrackSummary] =&gt; Your item was delivered in ITALY at 10:51 am on June 5, 2015. [TrackDetail] =&gt; Array ( [0] =&gt; Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALY [1] =&gt; Customs Clearance, June 4, 2015, 11:20 am, ITALY [2] =&gt; Processed Through Sort Facility, June 4, 2015, 11:19...
      • 我想 $deliveryStatus 有 xml 格式或者什么显示 print_r($deliveryStatus)?
      【解决方案3】:

      如果对某人有帮助:

      $deliveryStatus = $response->TrackInfo->TrackSummary;
      echo $deliveryStatus;
      

      【讨论】:

        猜你喜欢
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        相关资源
        最近更新 更多