Pergunta

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 .....

Foi útil?

Solução 2

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

Outras dicas

If the request is approaching the API then it would return the response which can be the proper expected response or the error code with some error detail.

The problem for not getting the proper response might be of the following here-

  1. Wrong XML passed to the query string
  2. Missed some required fields in the XML
  3. Invalid Web Tools Credentials

In order to know the exact Error Code and Details you need to parse the returned XML from the API Server.

You can get the response stream and read the stream to the Stream Reader

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

However I implemented this in .Net MVC but it might help for a head start to find the exact problem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top