xmlparser.php :
class xmlparser
{
function GetChildren($vals, &$i)
{
$children = array();
if (isset($vals[$i]['value']))
$children['VALUE'] = $vals[$i]['value'];
while (++$i < count($vals))
{
switch ($vals[$i]['type'])
{
case 'cdata':
if (isset($children['VALUE']))
$children['VALUE'] .= $vals[$i]['value'];
else
$children['VALUE'] = $vals[$i]['value'];
break;
case 'complete':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
if (isset($vals[$i]['value']))
$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
else
$children[$vals[$i]['tag']][$index]['VALUE'] = '';
} else {
if (isset($vals[$i]['value']))
$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
else
$children[$vals[$i]['tag']][]['VALUE'] = '';
}
break;
case 'open':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
} else {
$children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
break;
case 'close':
return $children;
}
}
}
function GetXMLTree($xml)
{
$data = $xml;
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
//print_r($index);
$tree = array();
$i = 0;
if (isset($vals[$i]['attributes'])) {
$tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($tree[$vals[$i]['tag']])-1;
$tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
}
else
$tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
return $tree;
}
function printa($obj) {
global $__level_deep;
if (!isset($__level_deep)) $__level_deep = array();
if (is_object($obj))
print '[obj]';
elseif (is_array($obj)) {
foreach(array_keys($obj) as $keys) {
array_push($__level_deep, "[".$keys."]");
$this->printa($obj[$keys]);
array_pop($__level_deep);
}
}
else print implode(" ",$__level_deep)." = $obj\n";
}
}
?>
==================================================
Next create "fedex.php" page
fedex.php :
require_once("xmlparser.php");
function bcdivs( $first, $second, $scale = 0 ) {
$res = $first / $second;
return round( $res, $scale );
}
class Fedex {
// Variables
var $server = "https://gatewaybeta.fedex.com/GatewayDC";
var $accountNumber;
var $meterNumber;
var $carrierCode = "FDXG";
var $dropoffType = "REGULARPICKUP";
var $service;
var $serviceName;
var $packaging = "YOURPACKAGING";
var $weightUnits = "LBS";
var $weight;
// Origin Address
var $originStateOrProvinceCode;
var $originPostalCode;
var $originCountryCode;
// Destination Address
var $destStateOrProvinceCode;
var $destPostalCode;
var $destCountryCode;
var $payorType = "SENDER";
// Functions
function setServer($server) {
$this->server = $server;
}
function setAccountNumber($accountNumber) {
$this->accountNumber = $accountNumber;
}
function setMeterNumber($meterNumber) {
$this->meterNumber = $meterNumber;
}
function setCarrierCode($carrierCode) {
$this->carrierCode = $carrierCode;
}
function setDropoffType($dropoffType) {
$this->dropoffType = $dropoffType;
}
function setService($service, $name) {
$this->service = $service;
$this->serviceName = $name;
}
function setPackaging($packaging) {
$this->packaging = $packaging;
}
function setWeightUnits($units) {
$this->weightUnits = $units;
}
function setWeight($weight) {
$this->weight = $weight;
}
function setOriginStateOrProvinceCode($code) {
$this->originStateOrProvinceCode = $code;
}
function setOriginPostalCode($code) {
$this->originPostalCode = $code;
}
function setOriginCountryCode($code) {
$this->originCountryCode = $code;
}
function setDestStateOrProvinceCode($code) {
$this->destStateOrProvinceCode = $code;
}
function setDestPostalCode($code) {
$this->destPostalCode = $code;
}
function setDestCountryCode($code) {
$this->destCountryCode = $code;
}
function setPayorType($type) {
$this->payorType = $type;
}
function getPrice() {
$str = '';
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$str .= '
$header[] = "Host: canvasgrafix.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;
$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);
$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;
}
}
}
class fedexError
{
var $number;
var $description;
var $response;
}
class fedexPrice
{
var $service;
var $rate;
var $response;
}
?>
==================================================
Next create "rate.php" page
rate.php :
function ship_rate() {
$dest_state = strtoupper($dest_state);
require_once('fedex.php');
$fedex = new Fedex;
$fedex->setServer("https://gatewaybeta.fedex.com/GatewayDC");
$fedex->setAccountNumber(#######); // You need your own
$fedex->setMeterNumber(########); // You need your own
$fedex->setCarrierCode("FDXG");
$fedex->setDropoffType("REGULARPICKUP");
$fedex->setService('FEDEXGROUND', 'FedEx Ground');
$fedex->setPackaging("YOURPACKAGING");
$fedex->setWeightUnits("LBS");
$fedex->setWeight("3.33");
$fedex->setOriginStateOrProvinceCode("MO");
$fedex->setOriginPostalCode(63110);
$fedex->setOriginCountryCode("US");
$fedex->setDestStateOrProvinceCode("MA");
$fedex->setDestPostalCode("02116");
$fedex->setDestCountryCode("US");
$fedex->setPayorType("SENDER");
$price = $fedex->getPrice();
$rate = ($price->price->rate + ($price->price->rate*1.00));
$rate = round($rate,2);
return $rate;
}
echo "Price : $".$vals=ship_rate();
?>
Now the Result will come from FEDEX.. Enjoy with this code..