如何使用USPS打印商品退货标签

时间:2013-07-24 11:08:13

标签: php shipping usps

好的usps建议发送请求xml到

secure.shippingapis.com/ShippingAPI.dll?API=MerchandiseReturnV4&XML=

我尝试了

中给出的示例XML
https://www.usps.com/business/web-tools-apis/merchandise-return-service-labels-v10-2a.htm


$url="https://secure.shippingapis.com/ShippingAPI.dll";
$api="API=MerchandiseReturnV4&XML=";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $api . $xml);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
$error = curl_error($ch);
    echo result;

但它什么也没有回来......

2 个答案:

答案 0 :(得分:1)

如果请求接近API,那么它将返回响应,该响应可能是正确的预期响应或错误代码,但有一些错误详细信息。

未获得正确答复的问题可能如下 -

  1. 错误的XML传递给查询字符串
  2. 错过了XML中的一些必填字段
  3. 无效的Web工具凭据
  4. 为了了解确切的错误代码和详细信息,您需要从API服务器解析返回的XML。

    您可以获取响应流并将流读取到Stream Reader

    string result = null;
    using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());
        result = reader.ReadToEnd();
    }
    

    但是我在.Net MVC中实现了这一点,但它可能有助于找到确切的问题。

答案 1 :(得分:0)

我在解决问题后忘记了答案。

连接到usps的功能: -

  public function connectToUSPS($url, $api, $xml) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $api . $xml);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $result = curl_exec($ch);
        $error = curl_error($ch);

        if (empty($error)) {
            return $result;
        } else {
            return false;
        }
    }

生成USPS标签的功能: -

  function create_UspsLabel($shipId, $userId) {
    $row = 'contains user data';
            $uspsUserName = 'a valid usps user name';
    $uspsPassword = 'a valid usps user password';

    $retailerName = 'retailer name';
    $retailerAddress ='retailer address';
    $permitNumber = 'a valid permit number';
    $retailerState = 'retailer state;
    $retailerCity = 'retailer city';
    $retailerZip = 'retailer zip code';

    $PDUPoBox = 'pdu box number';
    $PDUCity = 'pdu city name';
    $PDUState = 'pdu state name';
    $PDUZip = 'pdu zip code';
    $PDUZip2 = 'pdu zip code2';

    $oid = 'order id';
    $packageId = "KR" . str_pad(($oid + 1), 5, "0", STR_PAD_LEFT);

    $state = 'state';

    $xml = '<EMRSV4.0Request USERID="' . $uspsUserName . '" PASSWORD="' . $uspsPassword . '">
            <Option>RIGHTWINDOW</Option>
            <CustomerName>' . $row->name.' '.$row->lastName . '</CustomerName>
            <CustomerAddress1>' . $row->suit . '</CustomerAddress1>';

    $xml.=' <CustomerAddress2>' . $row->address1 . '</CustomerAddress2>
                            <CustomerCity>' . $row->city . '</CustomerCity>
            <CustomerState>' . $state . '</CustomerState>
            <CustomerZip5>' . $row->zipcode . '</CustomerZip5>

            <CustomerZip4 />
            <RetailerName>' . $retailerName . '</RetailerName>
            <RetailerAddress>' . $retailerAddress . '</RetailerAddress>
            <PermitNumber>' . $permitNumber . '</PermitNumber>
            <PermitIssuingPOCity>' . $retailerCity . '</PermitIssuingPOCity>
            <PermitIssuingPOState>' . $retailerState . '</PermitIssuingPOState>
            <PermitIssuingPOZip5>' . $retailerZip . '</PermitIssuingPOZip5>
            <PDUPOBox>' . $PDUPoBox . '</PDUPOBox>
            <PDUCity>' . $PDUCity . '</PDUCity>
            <PDUState>' . $PDUState . '</PDUState>
            <PDUZip5>' . $PDUZip . '</PDUZip5>
            <PDUZip4>' . $PDUZip2 . '</PDUZip4>
            <ServiceType>Priority Mail</ServiceType>
            <DeliveryConfirmation>False</DeliveryConfirmation>
            <InsuranceValue />
            <MailingAckPackageID>' . $packageId . '</MailingAckPackageID>
            <WeightInPounds>0</WeightInPounds>
            <WeightInOunces>10</WeightInOunces>
            <RMA>RMA 123456</RMA>
            <RMAPICFlag>False</RMAPICFlag>
            <ImageType>TIF</ImageType>
            <RMABarcode>False</RMABarcode>
            <AllowNonCleansedDestAddr>False</AllowNonCleansedDestAddr>
            </EMRSV4.0Request>';
    $result = $this->connectToUSPS('https://secure.shippingapis.com/ShippingAPI.dll', 'API=MerchandiseReturnV4&XML=', $xml);
    $xml = new SimpleXMLElement($result);
    $string = base64_decode($xml->MerchandiseReturnLabel);
    if ($string) {
        $img_file = fopen(__USPSLABELIMAGE__ . "uspsLabel.tif", "w");
        fwrite($img_file, $string);
        fclose($img_file);
        $imageMagickPath = "/usr/bin/convert";
        $dest = __USPSLABELIMAGE__ . "uspsLabel.tif";
        $filename = time() . ".png";
        $pngDestPath = __USPSLABELIMAGE__ . $filename;
        exec("$imageMagickPath -density 72 -channel RGBA -colorspace RGB -background none -fill none -dither None $dest $pngDestPath");
                    return $filename;
    } else {
        return "error";
    }
}