setUserInfo($userid,$pwd);
$this->setAccountInfo($actnumber,$zipcode);
$this->setShipKey($shipkey);
$this->_url = "https://ecommerce.airborne.com/apilanding.asp";
$this->_testurl = "https://ecommerce.airborne.com/apilandingtest.asp";
$this->dhlShipType = DHL_SHIP_PACKAGE;
$this->dhlBillType = DHL_BILL_SENDER;
$this->_xml = array();
}
function setUserInfo($userid,$pwd)
{
$this->_userid = $userid;
$this->_pwd = $pwd;
}
function setAccountInfo($actnumber,$zipcode)
{
$this->_actnumber = $actnumber;
$this->_zipcode = $zipcode;
}
function setShipKey($shipkey)
{
$this->_shipkey=$shipkey;
}
function setService($service,$sservice="")
{
$this->dhlService = $service;
$this->dhlSpecialService = $sservice;
}
function addShipmentDetail($shipdate,$weight,$length="",$width="",$height="",$shipmenttype=DHL_SHIP_PACKAGE,$addtype="",$addvalue=0)
{
$xml = "";
$xml.= "".$shipdate."";
$xml.= "".$this->dhlService."";
$xml.= "".$shipmenttype."";
$xml.= "".$weight."";
if($length>0 && $width>0 && $height>0)
{
$xml.="".$length."";
$xml.="".$width."";
$xml.="".$height."";
}
if(!empty($addtype) && $addvalue>0)
{
$xml.="".$addtype."";
$xml.="".$addtype."";
}
if(!empty($this->dhlSpecialService))
{
$xml.="".$this->dhlSpecialService."";
}
$xml.="";
$this->_xml['SHIPMENT']=$xml;
}
function setBillDetail($billtype=DHL_BILL_SENDER,$codpaycode="",$codpayvalue=0)
{
$xml = "";
$xml.="".$billtype."";
$xml.="".$this->_actnumber."";
if(!empty($codpaycode) && $codpayvalue >0)
$xml.="".$codpaycode."".$codpayvalue."";
$xml.="";
$this->_xml['BILL']=$xml;
}
function setSenderDetail($sentby,$phone)
{
$xml = "";
$xml.="".$sentby."";
$xml.="".$phone."";
$xml.="";
$this->_xml['SENDER']=$xml;
}
function setReceiverDetail($name,$state,$country,$zip,$city,$street,$streetline2,$phone,$company,$dept)
{
$xml.="";
if(!empty($company))
$xml.="".$company."";
if(!empty($dept))
$xml.="".$dept."";
if(!empty($street))
$xml.="".$street."";
if(!empty($streetline2))
$xml.="".$streetline2."";
if(!empty($city))
$xml.="".$city."";
$xml.="".$state."";
$xml.="".$zip."";
$xml.="".$country."";
$xml.="";
if(!empty($company))
$xml.="".$name."";
if(!empty($company))
$xml.="".$phone."";
$xml.="";
$this->_xml['REC'] = $xml;
}
function setProcessingInstructions($code)
{
$xml ="";
$xml.="$code";
$xml.="";
$this->_xml['INST'] = $xml;
}
function setLabelInstructions($imagetype,$code,$email,$message="")
{
$xml ="";
$xml.="";
if(!empty($code))
$xml.="$code";
if(!empty($email))
$xml.="$email$message";
$xml.="";
$this->_xml['INST'] = $xml;
}
function setOtherElement($data)
{
$xml="$data";
$this->_xml['OTHER'] = $xml;
}
function error()
{
return $this->_error;
}
function testShip()
{
$this->_isTest = true;
$this->_url = $this->_testurl;
}
function getShipKey()
{
if(!empty($shipkey))
return $shipkey;
else
return $this->requestShipKey();
}
function addShip()
{
$xml.= '';
$xml.= ''.$this->_shipkey.'';
$xml.= ''.$this->_actnumber.'';
$xml.= $this->_xml['SHIPMENT'];
if(empty($this->_xml['BILL']))
$this->setBillDetail();
$xml.= $this->_xml['BILL'];
$xml.= $this->_xml['REC'];
if(!empty($this->_xml['INST']))
$xml.= $this->_xml['INST'];
if(!empty($this->_xml['OTHER']))
$xml.= $this->_xml['OTHER'];
$xml.= '';
$this->_xml['SHIP'].=$xml;
}
function getShipRate()
{
if(empty($this->_xml['SHIP']))
$this->addShip();
//echo $xml; exit;
$xml = str_replace("--FUNCTION--",$this->_xml['SHIP'],$this->_getHeaderXML());
$this->_sendXML($xml);
$result = $this->_parseXML();
if($result==false) return false;
if(count($this->xmlData['TOTALCHARGEESTIMATE'])>1)
return $this->xmlData['TOTALCHARGEESTIMATE'];
else
return $this->xmlData['TOTALCHARGEESTIMATE'][0];
}
function getShipLabel()
{
$xml.= '';
$xml.= ''.$this->_shipkey.'';
$xml.= ''.$this->_actnumber.'';
$xml.= $this->_xml['SHIPMENT'];
if(empty($this->_xml['BILL']))
$this->setBillDetail();
$xml.= $this->_xml['BILL'];
$xml.= $this->_xml['SENDER'];
$xml.= $this->_xml['REC'];
if(!empty($this->_xml['INST']))
$xml.= $this->_xml['INST'];
if(!empty($this->_xml['OTHER']))
$xml.= $this->_xml['OTHER'];
$xml.= '';
//echo $xml; exit;
$xml = str_replace("--FUNCTION--",$xml,$this->_getHeaderXML());
$this->_sendXML($xml);
$result = $this->_parseXML();
if($result==false) return false;
$return =array();
$return['TRANSACTIONKEY']=$this->xmlData['AIRBILLNBR'][0];
if(isset($this->xmlData['IMAGE'][0]))
{
for($i=0;$ixmlData['IMAGE']);$i++)
$return['IMAGE'] .= trim($this->xmlData['IMAGE'][$i]);
$return['IMAGE'] = base64_decode($return['IMAGE']);
}
return $return;
}
function cancelShip($transactionkey)
{
$xml.= '';
$xml.= ''.$this->_shipkey.'';
$xml.= ''.$this->_actnumber.'';
$xml.= ''.$transactionkey.'';
$xml.= '';
//echo $xml; exit;
$xml = str_replace("--FUNCTION--",$xml,$this->_getHeaderXML());
$this->_sendXML($xml);
$result = $this->_parseXML();
if($result==false) return false;
return true;
}
function requestShipKey()
{
$xml.= '';
$xml.= ''.$this->_actnumber.'';
$xml.= ''.$this->_zipcode.'';
$xml.= '';
$xml = str_replace("--FUNCTION--",$xml,$this->_getHeaderXML());
//echo $xml;
$this->_sendXML($xml);
$result = $this->_parseXML();
if($result==false) return false;
return $this->xmlData['SHIPPINGKEY'][0];
}
function saveLable($name,$imgdata)
{
$fp = @fopen($name,"wb");
if(!is_resource($fp))
{
$this->_error = "Error : Can't save image.";
return false;
}
@fwrite($fp,$imgdata);
@fclose($fp);
}
/***********************************************************************
*** XML Parser - Callback functions ***
***********************************************************************/
function epXmlElementStart ($parser, $tag, $attributes) {
$this->currentTag = $tag;
/*$this->_parentnode['node'][$this->_parentnode[$parser]]=$tag;
$this->_parentnode[$parser]++;*/
}
function epXmlElementEnd ($parser, $tag) {
$this->currentTag = "";
//$this->_parentnode[$parser]--;
}
function epXmlData ($parser, $cdata) {
$this->xmlData[$this->currentTag][] = $cdata;
}
/********************************************************************************
*** DHL Private Functions *****
/********************************************************************************/
function _getHeaderXML()
{
$xml = '';
$xml.='';
$xml.='';
$xml.=''.$this->_userid.'';
$xml.=''.$this->_pwd.'';
$xml.='';
$xml.='--FUNCTION--';
$xml.='';
return $xml;
}
function _sendXML($xml)
{
global $config;
if(!$ch=curl_init())
{
$this->_error="Curl is not initialized.";
return false;
}
else
{
curl_setopt($ch, CURLOPT_URL,$this->_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($config['proxy']==1){
curl_setopt ($ch, CURLOPT_PROXY, $config['proxyHost'].":".$config['proxyPort']);
}
$this->_result = curl_exec($ch);
if(curl_error($ch) != "")
{
echo $this->_error= "Error with Curl installation: " . curl_error($ch) . "
";
return false;
}
else
{
curl_close($ch);
return $this->_result;
}
}
}
function _parseXML()
{
$this->_parser = xml_parser_create();
// Disable XML tag capitalisation (Case Folding)
xml_parser_set_option ($this->_parser, XML_OPTION_CASE_FOLDING, TRUE);
// Define Callback functions for XML Parsing
xml_set_object($this->_parser, &$this);
xml_set_element_handler ($this->_parser, "epXmlElementStart", "epXmlElementEnd");
xml_set_character_data_handler ($this->_parser, "epXmlData");
// Parse the XML response
xml_parse($this->_parser, $this->_result, TRUE);
if( xml_get_error_code( $this->_parser ) == XML_ERROR_NONE ) {
// Get the result into local variables.
//print_r($this->xmlData);
if(strpos($this->_result,"")!==false)
{
$myError = $this->xmlData["CODE"][0];
$myErrorMessage = $this->xmlData["DESC"][0];
$myErrorMessage.= $this->xmlData["DESCRIPTION"][0];
$this->_error.="Error($myError):".$myErrorMessage ;
return false;
}
return $this->xmlData;
} else {
// An XML error occured. Return the error message and number.
$myError = xml_get_error_code( $this->_parser ) + XML_ERROR_OFFSET;
$myErrorMessage = xml_error_string( $myError );
$this->_error="Error($myError):".$myErrorMessage ;
return false;
}
// Clean up our XML parser
xml_parser_free( $this->parser );
}
}
?>