USPS API - 创建标签后检索跟踪号C#

时间:2017-04-11 20:49:15

标签: c# api pdf ocr usps

我已成功使用USPS API创建运输标签。返回的XML为我提供了一个Delivery Confirmation Number,但它没有返回跟踪号。跟踪号码是我真正需要的。我已经使用(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文档,这些文档允许我使用交付确认号来检索跟踪号。我也找不到一个OCR,它将从base64编码的输出文件中读取跟踪号。

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

2 个答案:

答案 0 :(得分: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';

答案 1 :(得分:0)

事实证明,通过XML返回的递送确认编号可用作跟踪编号。只是在API创建的交付确认号码出现在USPS网站之前有一点滞后时间。