cyphix
06-16-2010, 10:19 PM
I am having a heck of a time getting the correct responses returned from the FedEx API when trying to track a package.
I have tried several different things to get it working but when I get a response back from FedEx it always say's the tracking number is invalid..... which is untrue.
I have even tried a 3rd party script called FedExDC.... while this works on SOME numbers, it doesn't work with others.
Here is some of the code I have tried..
// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 4.0.0
require_once('../library/fedex-common.php5');
//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = "../../TrackService_v4.wsdl";
//Set commonly used variables in fedex-common.php5. Set check to true.
if(setDefaults('check'))
{
$key=setDefaults('key');
$password=setDefaults('password');
$shipAccount=setDefaults('shipaccount');
$meter=setDefaults('meter');
$billAccount=setDefaults('billaccount');
$dutyAccount=setDefaults('dutyaccount');
}
//Set commonly used variables below. Set check to false.
else
{
$key='xxxxxxxxxxxxxxx';
$password='xxxxxxxxxxxxxxxxxxxxxxx';
$shipAccount='xxxxxxxxx';
$meter='xxxxxxxxxxx';
$billAccount='XXX';
$dutyAccount='XXX';
}
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information
$request['WebAuthenticationDetail'] = array('UserCredential' =>
array('Key' => $key, 'Password' => $password));
$request['ClientDetail'] = array('AccountNumber' => $shipAccount, 'MeterNumber' => $meter);
$request['TransactionDetail'] = array('CustomerTransactionId' => '*** Track Request v4 using PHP ***');
$request['Version'] = array('ServiceId' => 'trck', 'Major' => '4', 'Intermediate' => '0', 'Minor' => '0');
$request['PackageIdentifier'] = array('Value' => xxxxxxxxxx, // Replace 'XXX' with a valid tracking identifier
'Type' => 'TRACKING_NUMBER_OR_DOORTAG');
try
{
$response = $client ->track($request);
if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
{
printSuccess($client, $response);
}
else
{
printError($client, $response);
}
writeToLog($client); // Write to log file
} catch (SoapFault $exception) {
printFault($exception, $client);
}
Then in "fedex-common.php5" I have the standard FedEx code..
// Copyright 2009, FedEx Corporation. All rights reserved.
define('TRANSACTIONS_LOG_FILE', '../fedextransactions.log'); // Transactions log file
/**
* Print SOAP request and response
*/
define('Newline',"<br />");
function printSuccess($client, $response) {
echo '<h2>Transaction Successful</h2>';
echo "\n";
printRequestResponse($client);
}
function printRequestResponse($client){
echo '<h2>Request</h2>' . "\n";
echo '<pre>' . htmlspecialchars($client->__getLastRequest()). '</pre>';
echo "\n";
echo '<h2>Response</h2>'. "\n";
echo '<pre>' . htmlspecialchars($client->__getLastResponse()). '</pre>';
echo "\n";
}
/**
* Print SOAP Fault
*/
function printFault($exception, $client) {
echo '<h2>Fault</h2>' . "<br>\n";
echo "<b>Code:</b>{$exception->faultcode}<br>\n";
echo "<b>String:</b>{$exception->faultstring}<br>\n";
writeToLog($client);
}
/**
* SOAP request/response logging to a file
*/
function writeToLog($client){
if (!$logfile = fopen(TRANSACTIONS_LOG_FILE, "a"))
{
error_func("Cannot open " . TRANSACTIONS_LOG_FILE . " file.\n", 0);
exit(1);
}
fwrite($logfile, sprintf("\r%s:- %s",date("D M j G:i:s T Y"), $client->__getLastRequest(). "\n\n" . $client->__getLastResponse()));
}
//To use these defaults set the check to return true.
function setDefaults($var){
if($var == 'shipaccount') Return 'XXX';
/**
* If the billaccount and dutyaccount do not match the shipping account
* the pay type will need to be changed from SENDER in shipping transactions.
*/
if($var == 'billaccount') Return 'XXX';
if($var == 'dutyaccount') Return 'XXX';
if($var == 'meter') Return 'XXX';
if($var == 'key') Return 'XXX';
if($var == 'password') Return 'XXX';
if($var == 'check') Return false;
}
function printNotifications($notes){
foreach($notes as $noteKey => $note){
if(is_string($note)){
echo $noteKey . ': ' . $note . Newline;
}
else{
printNotifications($note);
}
}
echo Newline;
}
function printError($client, $response){
echo '<h2>Error returned in processing transaction</h2>';
echo "\n";
printNotifications($response -> Notifications);
printRequestResponse($client, $response);
}
Thanks for any help...... it is driving me crazy!
I have tried several different things to get it working but when I get a response back from FedEx it always say's the tracking number is invalid..... which is untrue.
I have even tried a 3rd party script called FedExDC.... while this works on SOME numbers, it doesn't work with others.
Here is some of the code I have tried..
// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 4.0.0
require_once('../library/fedex-common.php5');
//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = "../../TrackService_v4.wsdl";
//Set commonly used variables in fedex-common.php5. Set check to true.
if(setDefaults('check'))
{
$key=setDefaults('key');
$password=setDefaults('password');
$shipAccount=setDefaults('shipaccount');
$meter=setDefaults('meter');
$billAccount=setDefaults('billaccount');
$dutyAccount=setDefaults('dutyaccount');
}
//Set commonly used variables below. Set check to false.
else
{
$key='xxxxxxxxxxxxxxx';
$password='xxxxxxxxxxxxxxxxxxxxxxx';
$shipAccount='xxxxxxxxx';
$meter='xxxxxxxxxxx';
$billAccount='XXX';
$dutyAccount='XXX';
}
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information
$request['WebAuthenticationDetail'] = array('UserCredential' =>
array('Key' => $key, 'Password' => $password));
$request['ClientDetail'] = array('AccountNumber' => $shipAccount, 'MeterNumber' => $meter);
$request['TransactionDetail'] = array('CustomerTransactionId' => '*** Track Request v4 using PHP ***');
$request['Version'] = array('ServiceId' => 'trck', 'Major' => '4', 'Intermediate' => '0', 'Minor' => '0');
$request['PackageIdentifier'] = array('Value' => xxxxxxxxxx, // Replace 'XXX' with a valid tracking identifier
'Type' => 'TRACKING_NUMBER_OR_DOORTAG');
try
{
$response = $client ->track($request);
if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
{
printSuccess($client, $response);
}
else
{
printError($client, $response);
}
writeToLog($client); // Write to log file
} catch (SoapFault $exception) {
printFault($exception, $client);
}
Then in "fedex-common.php5" I have the standard FedEx code..
// Copyright 2009, FedEx Corporation. All rights reserved.
define('TRANSACTIONS_LOG_FILE', '../fedextransactions.log'); // Transactions log file
/**
* Print SOAP request and response
*/
define('Newline',"<br />");
function printSuccess($client, $response) {
echo '<h2>Transaction Successful</h2>';
echo "\n";
printRequestResponse($client);
}
function printRequestResponse($client){
echo '<h2>Request</h2>' . "\n";
echo '<pre>' . htmlspecialchars($client->__getLastRequest()). '</pre>';
echo "\n";
echo '<h2>Response</h2>'. "\n";
echo '<pre>' . htmlspecialchars($client->__getLastResponse()). '</pre>';
echo "\n";
}
/**
* Print SOAP Fault
*/
function printFault($exception, $client) {
echo '<h2>Fault</h2>' . "<br>\n";
echo "<b>Code:</b>{$exception->faultcode}<br>\n";
echo "<b>String:</b>{$exception->faultstring}<br>\n";
writeToLog($client);
}
/**
* SOAP request/response logging to a file
*/
function writeToLog($client){
if (!$logfile = fopen(TRANSACTIONS_LOG_FILE, "a"))
{
error_func("Cannot open " . TRANSACTIONS_LOG_FILE . " file.\n", 0);
exit(1);
}
fwrite($logfile, sprintf("\r%s:- %s",date("D M j G:i:s T Y"), $client->__getLastRequest(). "\n\n" . $client->__getLastResponse()));
}
//To use these defaults set the check to return true.
function setDefaults($var){
if($var == 'shipaccount') Return 'XXX';
/**
* If the billaccount and dutyaccount do not match the shipping account
* the pay type will need to be changed from SENDER in shipping transactions.
*/
if($var == 'billaccount') Return 'XXX';
if($var == 'dutyaccount') Return 'XXX';
if($var == 'meter') Return 'XXX';
if($var == 'key') Return 'XXX';
if($var == 'password') Return 'XXX';
if($var == 'check') Return false;
}
function printNotifications($notes){
foreach($notes as $noteKey => $note){
if(is_string($note)){
echo $noteKey . ': ' . $note . Newline;
}
else{
printNotifications($note);
}
}
echo Newline;
}
function printError($client, $response){
echo '<h2>Error returned in processing transaction</h2>';
echo "\n";
printNotifications($response -> Notifications);
printRequestResponse($client, $response);
}
Thanks for any help...... it is driving me crazy!