【问题标题】:USPS API - After creating labels retrieving Tracking Number C#USPS API - 创建标签后检索跟踪号 C#
【发布时间】:2017-04-11 20:49:15
【问题描述】:

我已经能够成功使用 USPS API 创建发货标签。返回的 XML 给了我一个交货确认号,但它不返回跟踪号。跟踪号是我真正需要的。我使用 (https://www.usps.com/business/web-tools-apis/delivery-confirmation-domestic-shipping-label-api.pdf) 作为我如何成功创建标签的基础,但没有地方提到如何获取跟踪号。

跟踪号位于标签中,可以从他们在 XML 输出中发送给您的 base64 代码创建。因此,为了解决我的问题,我尝试将标签保存为图像文件并使用 OCR 阅读器(MODI 和 Tesseract)读取跟踪号。那没有用。我收到的只是胡言乱语。

然后我尝试将输出另存为 PDF,并使用 iTextSharp 阅读 PDF。结果是它没有从文件中读取任何内容。

所以现在我在砖墙上。我从 USPS 中找不到任何 API 文档,这些文档允许我使用交付确认号来检索跟踪号。我也找不到可以从 base64 编码的输出文件中读取跟踪号的 OCR。

有没有人可以解决这个问题?提前致谢。

【问题讨论】:

    标签: c# api pdf ocr usps


    【解决方案1】:

    我也在寻找追踪号码。我成功创建了 PDF 并且有条形码编号但没有跟踪编号。

    下面是我的代码,它工作正常。

          $devurl = "https://secure.shippingapis.com/ShippingAPI.dll";
           $xml = rawurlencode('<DelivConfirmCertifyV4.0Request USERID="your User id">
            <Option>1</Option>
            <ImageParameters>
                <LabelSequence>
                  <PackageNumber>1</PackageNumber>
                  <TotalPackages>99</TotalPackages>
                </LabelSequence>
            </ImageParameters>
            <FromName>Joe Smith</FromName>
            <FromFirm>ABC Corp.</FromFirm>
            <FromAddress1>Apt. 3C</FromAddress1>
            <FromAddress2>6406 Ivy Lane</FromAddress2>
            <FromCity>Greenbelt</FromCity>
            <FromState>MD</FromState>
            <FromZip5>20770</FromZip5>
            <FromZip4>1234</FromZip4>
            <ToName>Tom XofY</ToName>
            <ToFirm>XYZ Corp.</ToFirm>
            <ToAddress1>Suite 4D</ToAddress1>
            <ToAddress2>8 Wildwood Drive</ToAddress2>
            <ToCity>Old Lyme</ToCity>
            <ToState>CT</ToState>
            <ToZip5>06371</ToZip5>
            <ToZip4></ToZip4>
            <WeightInOunces>2</WeightInOunces>
            <ServiceType>Priority</ServiceType>
            <InsuredAmount></InsuredAmount>
            <SeparateReceiptPage></SeparateReceiptPage>
            <POZipCode></POZipCode>
            <ImageType>PDF</ImageType>
            <LabelDate></LabelDate>
            <CustomerRefNo></CustomerRefNo>
            <AddressServiceRequested></AddressServiceRequested>
            <SenderName> Imtiyaz</SenderName>
            <SenderEMail>daleep.kbizsoft@gmail.com</SenderEMail>
            <RecipientName></RecipientName>
            <RecipientEMail></RecipientEMail>
            <Container>Variable</Container>
            <Size>Regular</Size>
            <CommercialPrice>True</CommercialPrice>
            <Content>
                <ContentType>LIVES</ContentType>
                <ContentDescription>Bees</ContentDescription>
            </Content>
        </DelivConfirmCertifyV4.0Request>'); 
    
        $service = 'DelivConfirmCertifyV4';
        $request = $devurl . "?API=" . $service . "&xml=" . $xml;
        $response = SendRequest($request);
        //print_r($response);
        $xml = new SimpleXMLElement($response);
        $myfile = fopen("trackingcreate.pdf", "w") or die("Unable to open file!");
        fwrite($myfile, base64_decode($xml->DeliveryConfirmationLabel)); 
        $json =json_encode($xml);
        $resarray  = json_decode($json);
        echo "<br><b>DeliveryConfirmationNumber :</b>  ".$resarray->DeliveryConfirmationNumber;
        echo  "<br><br><b>ToName : </b>  ".$resarray->ToName;
        echo " <br><br><b>ToFirm :  </b> ".$resarray->ToFirm;
        echo "<br><br><b>ToAddress1 : </b> ".$resarray->ToAddress1;
        echo "<br><br><b>ToCity :  </b> ".$resarray->ToCity;
        echo "<br><br><b>Postnet :  </b> ".$resarray->Postnet;
        echo "<br><br><b>RDC : </b>  ".$resarray->RDC;
        echo "<br><br><b>InsuranceFee :  </b> ".$resarray->InsuranceFee;
        echo  "<br><br><b>Postage : </b>  ".$resarray->Postage;
        echo "<br><br><b>CarrierRoute :  </b> ". $resarray->CarrierRoute;
    
        function SendRequest($request){
        $session = curl_init();
        curl_setopt($session, CURLOPT_URL, $request);
        curl_setopt($session, CURLOPT_HTTPGET, 1); 
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_SSL_VERIFYPEER , false );
        curl_setopt($session, CURLOPT_SSL_VERIFYHOST , false );
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($session);
        curl_close($session);
        return $response;
     } 
    

    请注意,显示的 API (DelivConfirmCertifyV4.0Request) 仅用于测试。要制作此生产代码,请进行以下两项更改:

    • 将开始和结束 XML 标记从 DelivConfirmCertifyV4.0Request 更改为 DeliveryConfirmationV4.0Request
    • 改变

      $service = 'DelivConfirmCertifyV4'; 到

      $service = 'DeliveryConfirmationV4';

    【讨论】:

    • 只需将其复制并粘贴到任何文件示例中即可 my folder/filename.php 运行代码,您将在 myfolder 中获得 pdf 文件 (trackingcreate.pdf)
    【解决方案2】:

    事实证明,通过 XML 返回的交货确认号可以用作跟踪号。只是 API 创建的 Delivery Confirmation Number 出现在 USPS 网站上之前有一点延迟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多