USPS API - After creating labels retrieving Tracking Number C#

╄→尐↘猪︶ㄣ 提交于 2019-12-24 06:39:06

问题


I have been able to successfully use the USPS API to create a shipping label. The XML returned gives me a Delivery Confirmation Number, but it does not return the tracking number. The tracking number is what I really need. I've used (https://www.usps.com/business/web-tools-apis/delivery-confirmation-domestic-shipping-label-api.pdf) as my base for how to successfully create the label, but no place does it mention how to get the tracking number.

The tracking number is in the label that can be created from the base64 code that they send you in the XML output. So to try to get around my problem I tried to save the label as an image file and use an OCR reader (both MODI & Tesseract) to read the tracking number. That did not work. All I received was gibberish.

So then I tried to save the output as a PDF instead and use iTextSharp to read the PDF. The result was it did not read anything from the file.

So now I'm at a brick wall. I cannot find any API documentation from USPS that would allow me to use the Delivery Confirmation Number to retrieve the tracking number. I also cannot find an OCR that will read the tracking number from the output files from the base64 encoding.

Is there anyone out there that might have a solution to this problem? Thanks in advance.


回答1:


I am also looking for the tracking number. I successfully create the PDF and there is a barcode number but no tracking number.

Below is my code and it works fine.

      $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;
 } 

Note the API shown (DelivConfirmCertifyV4.0Request) is for test only. To make this production code, make these two changes:

  • Change the beginning and end XML tag from DelivConfirmCertifyV4.0Request to DeliveryConfirmationV4.0Request
  • Change

    $service = 'DelivConfirmCertifyV4'; to

    $service = 'DeliveryConfirmationV4';




回答2:


It turns out that the delivery confirmation number that is returned via the XML can be used as the tracking number. It's just that there was a little bit of lag time before the API created Delivery Confirmation Number appeared on the USPS website.



来源:https://stackoverflow.com/questions/43355950/usps-api-after-creating-labels-retrieving-tracking-number-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!