|
Fedex Integrations
I am looking to hire someone who has experience in integrating a PHP application with FedEx shipping using a PHP class developed by Jay Powers @Vernmonster.com.
Thanks.
Arturo
aantonio@evaglobalinc.com
Below is a portion of the class code that i am reffering to:
here is the class code:
PHP Code:
php:
--------------------------------------------------------------------------------
<?php
/*
$Id: DirectConnect.pm,v 1.17 2003/07/07 13:53:01 jay.powers Exp $
Copyright (c) 2003 Vermonster LLC
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--------------------------------------------------------------------
FedEx-DirectConnect - PHP interface to FedEx Direct Connect API
This class has been developed to send transactions to FedEx's
Ship Manager Direct API. It can be used for all transactions
the API can support. For more detailed information please
referer to FedEx's documentation located at their website.
http://www.fedex.com/us/solutions/wis/. Here you will be able
to download "TagTransGuide.pdf" which outlines all the FedEx
codes needed to send calls to their API.
This class requires you have PHP CURL support.
To submit a transaction to FedEx's Gateway server you must have a valid
FedEx Account Number and a FedEx Meter Number. To gain access
and receive a Meter Number you must send a Subscribe () request to
FedEx containing your FedEx account number and contact information.
Questions, Comments
Jay Powers
jay@vermonster.com
Vermonster LLC
312 Stuart St.
Boston, MA 02116
*/
define('FEDEX_URI', 'https://gatewaybeta.fedex.com/GatewayDC', true);
define('FEDEX_HOST', 'gatewaybeta.fedex.com', true);
define('REQUEST_REFERER', 'www.dobius.com', true);
define('REQUEST_TIMEOUT', 10, true);
define('REQUEST_TYPE', 'CURL', true);
class FedExDC {
var $VERSION = '0.3';
var $NAME = 'FedExDC';
var $ERROR_STR = false;
//this will be the field returned by FedEx
//containing the binary image data
var $image_key;
// FedEx API URI
var $fedex_uri;
// referer host
var $referer;
// set the timeout
var $timeout;
/**
* load FedEx UTIs => Transaction Types, Applicable Carrier
*
* @var FE_TT
* @access public
*/
var $FE_TT = array (
'1002' => array ('007','FDXG'),
'1005' => array ('023','FDXE'),
'2016' => array ('021','FDXE'),
'2017' => array ('022','FDXE'),
'2018' => array ('019','FDXE'),
'2024' => array ('025',''),
'2025' => array ('410',''),
'3000' => array ('021','FDXG'),
'3001' => array ('023','FDXG'),
'3003' => array ('211',''),
'3004' => array ('022','FDXG'),
'5000' => array ('402',''),
'5001' => array ('405',''),
'5002' => array ('403','')
);
/**
* constructor: loads account# and meter#
*
* @param int $account FedEx Account number
* @param int $meter FedEx meter number
* @param array $params Associative array of parameters listed:
* fedex_uri: FedEx API URI
fedex_host: Host for FedEx
referer: Referering Host
timeout: Connection timeout in seconds.
*
* @access public
*/
function FedExDC ($account, $meter='', $params = array()) {
$this->account = $account;
$this->meter = $meter;
$this->time_start = $this->getmicrotime();
// param defaults
$this->fedex_uri = FEDEX_URI;
$this->fedex_host = FEDEX_HOST;
$this->referer = REQUEST_REFERER;
$this->timeout = REQUEST_TIMEOUT;
$this->image_key = 188;
foreach ($params as $key => $value) {
$this->{$key} = $value;
}
}
/**
* Sets debug information
*
* @param string $string debug data
* @access private
*/
function debug($string){
$this->debug_str .= get_class($this).": $string\n";
}
/**
* returns error string if present
*
* @return boolean string
* @access public
*/
function getError(){
if($this->ERROR_STR!= ""){
return $this->ERROR_STR;
}
return false;
}
/**
* sets error string
*
* @param string $str
* @access private
*/
function setError($str){
$this->ERROR_STR .= $str;
}
/**
* microtime
*
* @return float
* @access private
*/
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
/**
* creates FedEx buffer string
*
* @param int $uti FedEx transaction UTI
* @param array $vals values to send to FedEx
* @return string
* @access public
*/
function setData($uti, $vals) {
$this->sBuf = '';
if (empty($vals[0])) $vals[0] = $this->FE_TT[$uti][0];
if (empty($vals[3025])) $vals[3025] = $this->FE_TT[$uti][1];
if (isset($this->account) and!array_key_exists(10, $vals)) $this->sBuf .= '10,"' . $this->account . '"';
if (isset($this->meter) and!array_key_exists(498, $vals)) $this->sBuf .= '498,"' .$this->meter. '"';
foreach ($vals as $key => $val) {
if (preg_match('/^([0-9]+)\-?[0-9]?$/', $key)) { //let users use the hyphenated number fields
$this->sBuf .= "$key,\"$val\"";
} else { continue; }
}
$time = $this->getmicrotime() - $this->time_start;
$this->debug('setData: build FedEx string ('. $time.')');
//echo "<br><br>".$this->sBuf."<br><br>"; DEBUG
return $this->sBuf .= '99,""';
}
/**
* parses FedEx return string into assoc array
*
* @return array FedEx return values
* @access public
*/
function _splitData(){
$this->rHash = array();
$count=0;
$st_key = 0; // start the first key at 0
$aFedRet = preg_split('/,"/s', $this->httpBody);
foreach ($aFedRet as $chunk) {
preg_match('/(.*)"([\d+\-?]+)/s', $chunk, $match);
if (empty($match[1])) continue;
if ($st_key == 99) continue;
$this->rHash[$st_key] = $match[1];
$st_key = $match[2]; //this will be the next key
}
$time = $this->getmicrotime() - $this->time_start;
$this->debug('_splitData: Parse FedEx response ('. $time.')');
if ($this->rHash[2]) {
$this->setError("FedEx Return Error ". $this->rHash[2]." : ".$this->rHash[3]);
}
return $this->rHash;
}
/**
* decode binary label data
*
* @param string $label_file file to save label on disk
* @return mixed
* @access public
*/
function label($label_file=false) {
$this->httpLabel = $this->rHash[$this->image_key];
if ($this->httpLabel = preg_replace('/%([0-9][0-9])/e', "chr(hexdec($1))", $this->httpLabel)) {
$this->debug('separate binary image data');
$this->debug('decoded binary label data');
}
if ($label_file) {
$this->debug('label: trying to write out label to '. $label_file);
$FH = fopen ($label_file, "w+b");
if (!fwrite($FH, $this->httpLabel)) {
$this->setError("Can't write to file $label_file");
return false;
}
fclose($FH);
} else {
return $this->httpLabel;
}
}
/**
* prepares and sends request to FedEx API
*
* @param string $buf pre-formatted FedEx buffer
* @return mixed
* @access public
*/
/**
* FedEx service availability
*
* @param $aData array values to send to FedEx
* @return string
* @access private
*/
function service_avail ($aData) {
$this->setData(2018, $aData);
if ($aRet = $this->transaction()) {
return $aRet;
} else {
$this->setError('unable to process service_avail');
return false;
}
}
function sig_proof_delivery ($aData) {
$this->image_key = 1471;
$this->setData(5001, $aData);
if ($aRet = $this->transaction()) {
return $aRet;
} else {
$this->setError('unable to process sig_proof_delivery');
return false;
}
}
/**
* Track a shipment by tracking number
*
* @param $aData array values to send to FedEx
* @return string
* @access private
*/
function track ($aData) {
$this->setData(5000, $aData);
if ($aRet = $this->transaction()) {
return $aRet;
} else {
$this->setError('unable to process track');
return false;
}
}
/**
* Track By Number, Destination, Ship Date, and Reference
*
* @param $aData array values to send to FedEx
* @return string
* @access private
*/
function ref_track ($aData) {
$this->setData(5002, $aData);
if ($aRet = $this->transaction()) {
return $aRet;
} else {
$this->setError('unable to process ref_track');
return false;
}
}
}
?>
Last edited by Sepodati : February 5th, 2005 at 03:28 PM.
|