dp20011 2013-07-24 11:08
浏览 313
已采纳

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

well usps suggest send request xml to

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

I have tried with the example XML given at

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;

but it returning nothing .....

  • 写回答

2条回答 默认 最新

  • douyu7879 2014-03-25 12:26
    关注

    I forget to put the answers after solving my problem.

    Function for connect to 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;
            }
        }
    

    Function to generate USPS label :-

      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";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大