示例#1
0
文件: usps.php 项目: saiber/www
 function getPrice()
 {
     if ($this->country == "USA") {
         // may need to urlencode xml portion
         $str = $this->server . "?API=RateV3&XML=<RateV3Request%20USERID=\"";
         $str .= $this->user . "\"%20PASSWORD=\"" . $this->pass . "\"><Package%20ID=\"0\"><Service>";
         $str .= rawurlencode($this->service) . "</Service>";
         if (strtolower($this->service) == 'first class') {
             $str .= "<FirstClassMailType>PARCEL</FirstClassMailType>";
         }
         $str .= "<ZipOrigination>" . $this->orig_zip . "</ZipOrigination>";
         $str .= "<ZipDestination>" . $this->dest_zip . "</ZipDestination>";
         $str .= "<Pounds>" . $this->pounds . "</Pounds><Ounces>" . $this->ounces . "</Ounces>";
         $str .= "<Container>" . urlencode($this->container) . "</Container><Size>" . $this->size . "</Size>";
         $str .= "<Machinable>" . $this->machinable . "</Machinable>";
         $str .= "</Package></RateV3Request>";
     } else {
         $str = $this->server . "?API=IntlRate&XML=<IntlRateRequest%20USERID=\"";
         $str .= $this->user . "\"%20PASSWORD=\"" . $this->pass . "\"><Package%20ID=\"0\">";
         $str .= "<Pounds>" . $this->pounds . "</Pounds><Ounces>" . $this->ounces . "</Ounces>";
         $str .= "<MailType>Package</MailType><Country>" . urlencode($this->country) . "</Country></Package></IntlRateRequest>";
     }
     $ch = curl_init();
     // set URL and other appropriate options
     curl_setopt($ch, CURLOPT_URL, $str);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     // grab URL and pass it to the browser
     $ats = curl_exec($ch);
     // close curl resource, and free up system resources
     curl_close($ch);
     $xmlParser = new xmlparser();
     $array = $xmlParser->GetXMLTree($ats);
     $e = error_reporting();
     error_reporting(E_WARNING);
     //$xmlParser->printa($array);
     if (isset($array['ERROR'])) {
         // If it is error
         $error = new UspsError();
         $error->number = $array['ERROR'][0]['NUMBER'][0]['VALUE'];
         $error->source = $array['ERROR'][0]['SOURCE'][0]['VALUE'];
         $error->description = $array['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
         $error->helpcontext = $array['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
         $error->helpfile = $array['ERROR'][0]['HELPFILE'][0]['VALUE'];
         $this->error = $error;
     } else {
         if (isset($array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'])) {
             $error = new UspsError();
             $error->number = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['NUMBER'][0]['VALUE'];
             $error->source = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['SOURCE'][0]['VALUE'];
             $error->description = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
             $error->helpcontext = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
             $error->helpfile = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPFILE'][0]['VALUE'];
             $this->error = $error;
         } else {
             if (isset($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'])) {
                 //if it is international shipping error
                 $error = new UspsError($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR']);
                 $error->number = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['NUMBER'][0]['VALUE'];
                 $error->source = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['SOURCE'][0]['VALUE'];
                 $error->description = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
                 $error->helpcontext = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
                 $error->helpfile = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPFILE'][0]['VALUE'];
                 $this->error = $error;
             } else {
                 if (isset($array['RATEV3RESPONSE'])) {
                     // if everything OK
                     //print_r($array['RATEV2RESPONSE']);
                     $this->zone = $array['RATEV3RESPONSE'][0]['PACKAGE'][0]['ZONE'][0]['VALUE'];
                     foreach ($array['RATEV3RESPONSE'][0]['PACKAGE'][0]['POSTAGE'] as $value) {
                         $price = new UspsPrice();
                         $price->mailservice = $value['MAILSERVICE'][0]['VALUE'];
                         $price->rate = $value['RATE'][0]['VALUE'];
                         $this->list[] = $price;
                     }
                 } else {
                     if (isset($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['SERVICE'])) {
                         // if it is international shipping and it is OK
                         foreach ($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['SERVICE'] as $value) {
                             $price = new UspsIntPrice();
                             $price->id = $value['ATTRIBUTES']['ID'];
                             $price->pounds = $value['POUNDS'][0]['VALUE'];
                             $price->ounces = $value['OUNCES'][0]['VALUE'];
                             $price->mailtype = $value['MAILTYPE'][0]['VALUE'];
                             $price->country = $value['COUNTRY'][0]['VALUE'];
                             $price->rate = $value['POSTAGE'][0]['VALUE'];
                             $price->svccommitments = $value['SVCCOMMITMENTS'][0]['VALUE'];
                             $price->svcdescription = $value['SVCDESCRIPTION'][0]['VALUE'];
                             $price->maxdimensions = $value['MAXDIMENSIONS'][0]['VALUE'];
                             $price->maxweight = $value['MAXWEIGHT'][0]['VALUE'];
                             $this->list[] = $price;
                         }
                     }
                 }
             }
         }
     }
     //if (isset($error)) { echo '<pre>'; print_r($array); echo '</pre>';  }
     error_reporting($e);
     return $this;
 }
 function getPrice()
 {
     /*$str = " ";
     $str .= ' ';
     $str .= ' ';
     $str .= 'Express Rate';
     $str .= ' '.$this->accountNumber."";
     $str .= ' '.$this->meterNumber."";
     $str .= ' '.$this->carrierCode."";
     $str .= ' ';
     $str .= ' '.$this->dropoffType."";
     $str .= ' '.$this->service."";
     $str .= ' '.$this->packaging."";
     $str .= ' '.$this->weightUnits."";
     $str .= ' '.number_format($this->weight, 1, '.', "");
     $str .= ' ';
     $str .= ' '.$this->originStateOrProvinceCode;
     $str .= ' '.$this->originPostalCode;
     $str .= ' '.$this->originCountryCode;
     $str .= ' ';
     $str .= ' ';
     $str .= ' '.$this->destStateOrProvinceCode;
     $str .=  ' ' .$this->destPostalCode;
     $str .= ' '.$this->destCountryCode;
     $str .= ' ';
     $str .= ' ';
     $str .= ' '.$this->payorType;
     $str .= ' ';
     $str .= ' '.ceil(bcdivs(number_format($this->weight, 1, '.',""), '150', 3));
     $str .= ' ';*/
     $str = '<?xml version="1.0" encoding="UTF-8" ?>';
     $str .= '    <FDXRateRequest xmlns:api="http://www.fedex.com/fsmapi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXRateRequest.xsd">';
     $str .= '        <RequestHeader>';
     $str .= '            <CustomerTransactionIdentifier>Express Rate</CustomerTransactionIdentifier>';
     $str .= '            <AccountNumber>' . $this->accountNumber . '</AccountNumber>';
     $str .= '            <MeterNumber>' . $this->meterNumber . '</MeterNumber>';
     $str .= '            <CarrierCode>' . $this->carrierCode . '</CarrierCode>';
     $str .= '        </RequestHeader>';
     $str .= '        <DropoffType>' . $this->dropoffType . '</DropoffType>';
     $str .= '        <Service>' . $this->service . '</Service>';
     $str .= '        <Packaging>' . $this->packaging . '</Packaging>';
     $str .= '        <WeightUnits>' . $this->weightUnits . '</WeightUnits>';
     $str .= '        <Weight>' . number_format($this->weight, 1, '.', '') . '</Weight>';
     $str .= '        <OriginAddress>';
     $str .= '            <StateOrProvinceCode>' . $this->originStateOrProvinceCode . '</StateOrProvinceCode>';
     $str .= '            <PostalCode>' . $this->originPostalCode . '</PostalCode>';
     $str .= '            <CountryCode>' . $this->originCountryCode . '</CountryCode>';
     $str .= '        </OriginAddress>';
     $str .= '        <DestinationAddress>';
     $str .= '            <StateOrProvinceCode>' . $this->destStateOrProvinceCode . '</StateOrProvinceCode>';
     $str .= '            <PostalCode>' . $this->destPostalCode . '</PostalCode>';
     $str .= '            <CountryCode>' . $this->destCountryCode . '</CountryCode>';
     $str .= '        </DestinationAddress>';
     $str .= '        <Payment>';
     $str .= '            <PayorType>' . $this->payorType . '</PayorType>';
     $str .= '        </Payment>';
     $str .= '        <PackageCount>' . ceil(bcdiv(number_format($this->weight, 1, '.', ''), '150', 3)) . '</PackageCount>';
     $str .= '    </FDXRateRequest>';
     $header[] = "Host: iscripts.com";
     $header[] = "MIME-Version: 1.0";
     $header[] = "Content-type: multipart/mixed; boundary=—-doc";
     $header[] = "Accept: text/xml";
     $header[] = "Content-length: " . strlen($str);
     $header[] = "Cache-Control: no-cache";
     $header[] = "Connection: close \r\n";
     $header[] = $str;
     //echo 'in';exit;
     $ch = curl_init();
     //Disable certificate check.
     // uncomment the next line if you get curl error 60: error setting certificate verify locations
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     // uncommenting the next line is most likely not necessary in case of error 60
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     //————————-
     //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
     //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     //curl_setopt($ch, CURLOPT_CAINFO, "c:/ca-bundle.crt");
     //————————-
     curl_setopt($ch, CURLOPT_URL, $this->server);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_TIMEOUT, 4);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     //echo '<br> server :'. $this->server;
     //echo '<br> header :'. print_r($header);
     $data = curl_exec($ch);
     //print_r($data);
     if (curl_errno($ch)) {
         $this->getPrice();
     } else {
         // close curl resource, and free up system resources
         curl_close($ch);
         $xmlParser = new xmlparser();
         $array = $xmlParser->GetXMLTree($data);
         //$xmlParser->printa($array);
         if (!empty($array['FDXRATEREPLY'])) {
             if (!empty($array['FDXRATEREPLY'][0]['ERROR'])) {
                 // If it is error
                 $error = new fedexError();
                 $error->number = $array['FDXRATEREPLY'][0]['ERROR'][0]['CODE'][0]['VALUE'];
                 $error->description = $array['FDXRATEREPLY'][0]['ERROR'][0]['MESSAGE'][0]['VALUE'];
                 $error->response = $array;
                 $this->error = $error;
             } else {
                 if (count($array['FDXRATEREPLY'][0]['ESTIMATEDCHARGES'][0]['DISCOUNTEDCHARGES'][0]['NETCHARGE'])) {
                     $price = new fedexPrice();
                     $price->rate = $array['FDXRATEREPLY'][0]['ESTIMATEDCHARGES'][0]['DISCOUNTEDCHARGES'][0]['NETCHARGE'][0]['VALUE'];
                     $price->service = $this->serviceName;
                     $price->response = $array;
                     $this->price = $price;
                 }
             }
         }
         //print_r($this); echo " Test1: "; exit();
         return $this;
     }
 }
 private function getArrayFromXML($xml)
 {
     $parser = new xmlparser();
     return $parser->GetXMLTree($xml);
 }