Fedex Tracking Api Using PHP

, ,

So I was trying to track down the way to use the FedEx API and Developer Tools to get tracking information.  Found the API’s Documentation a little hard to follow but thankfully I came across an article that gave a nice sample script.  Thank you for providing the following code.

<?php
// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 6.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_v9.wsdl";
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' => 'KUFWQTFzJsgy48If ',
		'Password' => 'vQjjZJGVbXurqNN2kceq0Z9lr'
	)
);
$request['ClientDetail'] = array(
	'AccountNumber' => '510087143',
	'MeterNumber' => '107487844'
);
$request['TransactionDetail'] = array('CustomerTransactionId' => '*** Track Request using PHP ***');
$request['Version'] = array(
	'ServiceId' => 'trck',
	'Major' => '9',
	'Intermediate' => '1',
	'Minor' => '0'
);
$request['SelectionDetails'] = array(
	'PackageIdentifier' => array(
		'Type' => 'TRACKING_NUMBER_OR_DOORTAG',
		'Value' => '624108940304' // Replace 'XXX' with a valid tracking identifier
	)
);
$track = $client ->track($request);
echo '<pre>';
print_r($client ->track($request));
echo '</pre>';
$info['Tracking number'] = $track->CompletedTrackDetails->TrackDetails->TrackingNumber;
$info['Reference'] = $track->CompletedTrackDetails->TrackDetails->OtherIdentifiers['1']->PackageIdentifier->Value;
$info['Door tag number'] = $track->CompletedTrackDetails->TrackDetails->OtherIdentifiers['2']->PackageIdentifier->Value;
$info['Dimensions'] = $track->CompletedTrackDetails->TrackDetails->PackageWeight->Value.'X'.$track->CompletedTrackDetails->TrackDetails->PackageDimensions->Width.'X'.$track->CompletedTrackDetails->TrackDetails->PackageDimensions->Height.' '.$track->CompletedTrackDetails->TrackDetails->PackageDimensions->Units;
$info['Total shipment weight'] = $track->CompletedTrackDetails->TrackDetails->ShipmentWeight->Value.' '.$track->CompletedTrackDetails->TrackDetails->ShipmentWeight->Units;
$info['Service'] = $track->CompletedTrackDetails->TrackDetails->OperatingCompanyOrCarrierDescription;
$info['Master tracking number'] = $track->CompletedTrackDetails->TrackDetails->TrackingNumber;
$info['Weight'] = $track->CompletedTrackDetails->TrackDetails->PackageWeight->Value.' '.$track->CompletedTrackDetails->TrackDetails->PackageWeight->Units;
$info['Total pieces'] = $track->CompletedTrackDetails->TrackDetails->TrackingNumber;
try {
	if(setEndpoint('changeEndpoint')){
		$newLocation = $client->__setLocation(setEndpoint('endpoint'));
	}
	$response = $client ->track($request);
    if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR'){
		if($response->HighestSeverity != 'SUCCESS'){
			echo '<table border="1">';
			echo '<tr><th>Track Reply</th><th>&nbsp;</th></tr>';
			trackDetails($response->Notifications, '');
			echo '</table>';
		}else{
	    	if ($response->CompletedTrackDetails->HighestSeverity != 'SUCCESS'){
				echo '<table border="1">';
			    echo '<tr><th>Shipment Level Tracking Details</th><th>&nbsp;</th></tr>';
			    trackDetails($response->CompletedTrackDetails, '');
				echo '</table>';
			}else{
				echo '<table border="1">';
			    echo '<tr><th>Package Level Tracking Details</th><th>&nbsp;</th></tr>';
			    trackDetails($response->CompletedTrackDetails->TrackDetails, '');
				echo '</table>';
			}
		}
        printSuccess($client, $response);
    }else{
        printError($client, $response);
    }
    writeToLog($client);    // Write to log file
} catch (SoapFault $exception) {
    printFault($exception, $client);
}
?>

Fedex Tracking Api Using PHP

Skills

Posted on

July 11, 2016

Submit a Comment

Your email address will not be published. Required fields are marked *