0

那么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;

但它什么也没返回......

4

2 回答 2

1

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

没有得到正确响应的问题可能是以下问题 -

  1. 传递给查询字符串的 XML 错误
  2. 遗漏了 XML 中的一些必填字段
  3. 无效的 Web 工具凭据

为了知道确切的错误代码和详细信息,您需要解析从 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 中实现了这一点,但它可能有助于抢先找到确切的问题。

于 2014-03-25T11:53:40.273 回答
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";
    }
}
于 2014-03-25T12:26:25.517 回答