php code for fedex shipment

<?php

// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 12.0.0

require_once(‘ShipWebServiceClient-fedex-common.php’);
include ‘../../config/config.php’;

$idorder = htmlspecialchars($_POST[“idorder”]);
$Weight2  = htmlspecialchars($_POST[“weight”]);
$Length1  = htmlspecialchars($_POST[“length”]);
$Width1   = htmlspecialchars($_POST[“width”]);
$Height1  = htmlspecialchars($_POST[“height”]);
$ServiceType = htmlspecialchars($_POST[“ServiceType”]);
//Shipper Information
$pShipperName  = htmlspecialchars($_POST[“pShipperName”]);
$pShipperCompany  = htmlspecialchars($_POST[“pShipperCompany”]);
$pShipperPhoneNumber  = htmlspecialchars($_POST[“pShipperPhoneNumber”]);
$pShipperAddress  = htmlspecialchars($_POST[“pShipperAddress”]);
$pShipperZip  = htmlspecialchars($_POST[“pShipperZip”]);
$pShipperCity  = htmlspecialchars($_POST[“pShipperCity”]);
$pShipperState  = htmlspecialchars($_POST[“pShipperState”]);
$pShipperCountryCode  = htmlspecialchars($_POST[“pShipperCountryCode”]);

if($ServiceType==”FEDEX_GROUND”)
{
$do_simple=1;
}
else if($ServiceType==”INTERNATIONAL_PRIORITY”)
{
$do_simple=2;
}
else
{
$do_simple=0;
}

//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = “wsdl/ShipService_v12.wsdl”;

//define(‘SHIP_LABEL’, ‘shipexpresslabel.pdf’);  // PDF label file. Change to file-extension .pdf for creating a PDF label (e.g. shiplabel.pdf)

$sql = “usp_userdetail_G $idorder”;
$r = sqlsrv_query(conn,$sql) or die ( ‘Query Error’ );
//define(‘r’,$r);

while($row = sqlsrv_fetch_array($r))
{
$ToName=$row[“FName”];
$tolastname    =$row[“LName”];
$ToCompany=$row[“customerCompany”];
$ToAddress1=$row[“CAddress”];
$ToCity =$row[“City”];
$ToState =$row[“State”];
$ToPostalCode=$row[“Zip”];
$ToPhone = $row[“Phone”];
}
//Sender Information

define(‘shipper_Name’,$pShipperName);
define(‘shipperCompanyName’,$pShipperCompany);
define(‘shipperPhoneNumber’,$pShipperPhoneNumber);
define(‘shipperAddressLine1’,$pShipperAddress);
define(‘shipperCity’,$pShipperCity);
define(‘shipperStateOrProvinceCode’,$pShipperState);
define(‘shipperPostalCode’,$pShipperZip);
define(‘shipperCountryCode’,$pShipperCountryCode);

//Receptent information
define(‘RecipientName’,$ToName.’ ‘.$tolastname);
define(‘RecipientCompanyName’,$ToCompany);
define(‘RecipientPhoneNumber’,$ToPhone);
define(‘RecipientAddressLine1’,$ToAddress1);
define(‘RecipientCity’,$ToCity);
define(‘RecipientStateOrProvinceCode’,$ToState);
define(‘RecipientPostalCode’,$ToPostalCode);
define(‘Weight2’,$Weight2);
define(‘Length1’,$Length1);
define(‘Width1’,$Width1);
define(‘Height1’,$Height1);
define(‘InvoiceNumber’,$idorder);

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’ => getProperty(‘key’),
‘Password’ => getProperty(‘password’)
)
);
$request[‘ClientDetail’] = array(
‘AccountNumber’ => getProperty(‘shipaccount’),
‘MeterNumber’ => getProperty(‘meter’)
);
$request[‘TransactionDetail’] = array(‘CustomerTransactionId’ => ‘*** Ground Domestic Shipping Request v12 using PHP ***’);
$request[‘Version’] = array(
‘ServiceId’ => ‘ship’,
‘Major’ => ’12’,
‘Intermediate’ => ‘1’,
‘Minor’ => ‘0’
);

$complex= array(
‘ShipTimestamp’ => date(‘c’),
‘DropoffType’ => ‘REGULAR_PICKUP’, // valid values REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER and STATION
‘ServiceType’ => $ServiceType,//’FEDEX_GROUND’, // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, …
‘PackagingType’ => ‘YOUR_PACKAGING’, // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, …
‘Shipper’ => addShipper(),
‘Recipient’ => addRecipient(),
‘ShippingChargesPayment’ => addShippingChargesPayment(),
‘LabelSpecification’ => addLabelSpecification(),
/* Thermal Label */
/*
‘LabelSpecification’ => array(
‘LabelFormatType’ => ‘COMMON2D’, // valid values COMMON2D, LABEL_DATA_ONLY
‘ImageType’ => ‘EPL2’, // valid values DPL, EPL2, PDF, ZPLII and PNG
‘LabelStockType’ => ‘STOCK_4X6.75_LEADING_DOC_TAB’,
‘LabelPrintingOrientation’ => ‘TOP_EDGE_OF_TEXT_FIRST’
),
*/
‘CustomerSpecifiedDetail’ => array(
‘MaskedData’=> ‘SHIPPER_ACCOUNT_NUMBER’
),
‘RateRequestTypes’ => array(‘ACCOUNT’), // valid values ACCOUNT and LIST
‘PackageCount’ => 1,
‘PackageDetail’ => ‘INDIVIDUAL_PACKAGES’,
‘RequestedPackageLineItems’ => array(
‘0’ => addPackageLineItem2()
), ‘SpecialServicesRequested’ => addSpecialServices()
);

$complex1= array(
‘ShipTimestamp’ => date(‘c’),
‘DropoffType’ => ‘REGULAR_PICKUP’, // valid values REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER and STATION
‘ServiceType’ => $ServiceType,//’FEDEX_GROUND’, // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, …
‘PackagingType’ => ‘YOUR_PACKAGING’, // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, …
‘Shipper’ => addShipper(),
‘Recipient’ => addRecipient(),
‘ShippingChargesPayment’ => addShippingChargesPayment(),
‘LabelSpecification’ => addLabelSpecification(),
/* Thermal Label */
/*
‘LabelSpecification’ => array(
‘LabelFormatType’ => ‘COMMON2D’, // valid values COMMON2D, LABEL_DATA_ONLY
‘ImageType’ => ‘EPL2’, // valid values DPL, EPL2, PDF, ZPLII and PNG
‘LabelStockType’ => ‘STOCK_4X6.75_LEADING_DOC_TAB’,
‘LabelPrintingOrientation’ => ‘TOP_EDGE_OF_TEXT_FIRST’
),
*/
‘CustomerSpecifiedDetail’ => array(
‘MaskedData’=> ‘SHIPPER_ACCOUNT_NUMBER’
),
‘RateRequestTypes’ => array(‘ACCOUNT’), // valid values ACCOUNT and LIST
‘PackageCount’ => 1,
‘PackageDetail’ => ‘INDIVIDUAL_PACKAGES’,
‘RequestedPackageLineItems’ => array(
‘0’ => addPackageLineItem1()
)
);

$complex2 =  array(
‘ShipTimestamp’ => date(‘c’),
‘DropoffType’ => ‘REGULAR_PICKUP’, // valid values REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER and STATION
‘ServiceType’ => $ServiceType, // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, …
‘PackagingType’ => ‘YOUR_PACKAGING’, // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, …
‘Shipper’ => addShipper(),
‘Recipient’ => addRecipient(),
‘ShippingChargesPayment’ => addShippingChargesPayment(),
‘CustomsClearanceDetail’ => addCustomClearanceDetail(),
‘LabelSpecification’ => addLabelSpecification(),
‘CustomerSpecifiedDetail’ => array(
‘MaskedData’=> ‘SHIPPER_ACCOUNT_NUMBER’
),
‘RateRequestTypes’ => array(‘ACCOUNT’), // valid values ACCOUNT and LIST
‘PackageCount’ => 1,
‘RequestedPackageLineItems’ => array(
‘0’ => addPackageLineItem2()
), ‘SpecialServicesRequested’ => addSpecialServices(),
‘CustomerReferences’ => array(
‘0’ => array(
‘CustomerReferenceType’ => ‘CUSTOMER_REFERENCE’,
‘Value’ => ‘TC007_07_PT1_ST01_PK01_SNDUS_RCPCA_POS’
)
)
);

if($do_simple!=1)
{
$request[‘RequestedShipment’]=$complex;
}
else if($do_simple == 2)
{
$request[‘RequestedShipment’]=$complex2;
}
else
{
$request[‘RequestedShipment’]=$complex1;
}

try
{
if(setEndpoint(‘changeEndpoint’))
{
$newLocation = $client->__setLocation(setEndpoint(‘endpoint’));
}

$response = $client->processShipment($request); // FedEx web service invocation

//var_dump($response);

$tracking_number=$response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;

//var_dump($response->CompletedShipmentDetail->CompletedPackageDetails);

//var_dump($s->TrackingIds->TrackingNumber);

//foreach($track as $m){$m;}
//$tracking_number=$m;
//var_dump($s->PackageRating);

//var_dump($tmp);

/*echo “———————–“;
var_dump($tmp[0]->NetCharge->Amount);

echo “<br>”;

echo $tmp[0]->NetCharge->Amount;
*/

//echo $amount=explode(‘”‘,substr(explode(‘NetCharge’,explode(‘[‘,substr($data,0,800))[1])[1],0,40))[8];

//$mid_amount=explode(‘[‘,substr($amount,0,800));
//$mid_amount1=;

//$amount=explode(‘”‘,substr(explode(‘NetCharge’,explode(‘[‘,substr($data,0,800))[1])[1],0,40))[8];

if ($response->HighestSeverity != ‘FAILURE’ && $response->HighestSeverity != ‘ERROR’)
{
printSuccess($client, $response);

//$trackingnumber = $response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;//Get tracking number

if($do_simple==0)
{

$trackingnumber = $tracking_number;//Get tracking number
$netshipcharge = $net_charge=$response->CompletedShipmentDetail->CompletedPackageDetails->PackageRating->PackageRateDetails[0]->NetCharge->Amount;
}
else
{
$trackingnumber = $response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;
$netshipcharge  =  $response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails->TotalNetFedExCharge->Amount;
}
//$netshipcharge  =  $response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails->TotalNetFedExCharge->Amount;
//$response->CompletedShipmentDetail->ShipmentRating->ShipmentRateDetails->TotalNetFedExCharge->Amount;
echo $trackingnumber.”<br/>”;
//echo $netshipcharge.”<br/>”;

define(‘SHIP_LABEL’, ‘Labels/’.$idorder.”._.”.$trackingnumber.”._.’fedexGroundshipgroundlabel.pdf’);  // PDF label file.
define(‘SHIP_CODLABEL’, ‘Labels/’.$idorder.”._.”.$trackingnumber.”._.’fedexGroundCODgroundreturnlabel.png’);  // PDF label file.

$sql1 = “update carthead set trackingId=” where idorder=$idorder”;

//  echo  $sql1 = “update carthead set trackingId=’$trackingnumber’ , trackingidGenratedOn=getdate(),Weight=$Weight2,Length=$Length1,Width=$Width1,Height=$Height1,fedexShippingCharge=$netshipcharge,ServiceType=’$ServiceType’ where idorder=$idorder”;

//$r = sqlsrv_query ( conn,$sql1 ) or die ( ‘Query Error’ );

//echo SHIP_LABEL1;

$fp = fopen(SHIP_CODLABEL, ‘wb’);
fwrite($fp, $response->CompletedShipmentDetail->CompletedPackageDetails->CodReturnDetail->Label->Parts->Image); //Create COD Return PNG or PDF file
fclose($fp);
//echo ‘<a href=”./’.SHIP_CODLABEL.'”>’.SHIP_CODLABEL.'</a> was generated.’.Newline;

// Create PNG or PDF label
// Set LabelSpecification.ImageType to ‘PNG’ for generating a PNG label

$fp = fopen(SHIP_LABEL, ‘wb’);
fwrite($fp, ($response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
fclose($fp);

define(‘PICKUP_SHIP_LABEL’, ‘test/shipexpresslabel.pdf’);

$fp = fopen(PICKUP_SHIP_LABEL, ‘wb’);
fwrite($fp, ($response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
fclose($fp);

//echo ‘<a href=”./’.SHIP_LABEL.'”>’.SHIP_LABEL.'</a> was generated.’;
//header(“Location: ../../../../../../admin/getshipboxdetail.asp?Result=shipment created&idorder=$idorder”);

//echo “<a href=’/ship/fedex/”.SHIP_LABEL.”‘></a>”;

}
else
{
printError($client, $response);
}

writeToLog($client);    // Write to log file

} catch (SoapFault $exception) {
printFault($exception, $client);
}

function addShipper(){
$shipper = array(
‘Contact’ => array(
‘PersonName’ => shipper_Name,
‘CompanyName’ => shipperCompanyName,
‘PhoneNumber’ => shipperPhoneNumber),
‘Address’ => array(
‘StreetLines’ => array(shipperAddressLine1),
‘City’ => shipperCity,
‘StateOrProvinceCode’ => shipperStateOrProvinceCode,
‘PostalCode’ => shipperPostalCode,
‘CountryCode’ => shipperCountryCode)
);

return $shipper;
}
function addRecipient(){
$recipient = array(
‘Contact’ => array(
‘PersonName’ => RecipientName,
‘CompanyName’ => RecipientCompanyName,
‘PhoneNumber’ => RecipientPhoneNumber
),
‘Address’ => array(
‘StreetLines’ => array(RecipientAddressLine1),
‘City’ => RecipientCity,
‘StateOrProvinceCode’ => RecipientStateOrProvinceCode,
‘PostalCode’ => RecipientPostalCode,
‘CountryCode’ => ‘US’,
‘Residential’ => true
)
);
return $recipient;
}
function addShippingChargesPayment(){
$shippingChargesPayment = array(‘PaymentType’ => ‘SENDER’,
‘Payor’ => array(
‘ResponsibleParty’ => array(
‘AccountNumber’ => getProperty(‘billaccount’),
‘Contact’ => null,
‘Address’ => array(‘CountryCode’ => ‘US’))));
return $shippingChargesPayment;
}
function addLabelSpecification(){
$labelSpecification = array(
‘LabelFormatType’ => ‘COMMON2D’, // valid values COMMON2D, LABEL_DATA_ONLY
‘ImageType’ => ‘PNG’,  // valid values DPL, EPL2, PDF, ZPLII and PNG
‘LabelStockType’ => ‘PAPER_4X9’);
return $labelSpecification;
}
function addSpecialServices(){
$specialServices = array(
‘SpecialServiceTypes’ => array(‘COD’),
‘CodDetail’ => array(
‘CodCollectionAmount’ => array(‘Currency’ => ‘USD’, ‘Amount’ => 1),
‘CollectionType’ => ‘ANY’)// ANY, GUARANTEED_FUNDS
);
return $specialServices;
}
function addSpecialServices_ORIGINAL(){
$specialServices = array(
‘SpecialServiceTypes’ => array(‘COD’),
‘CodDetail’ => array(
‘CodCollectionAmount’ => array(‘Currency’ => ‘USD’, ‘Amount’ => 150),
‘CollectionType’ => ‘ANY’)// ANY, GUARANTEED_FUNDS
);
return $specialServices;
}

function addCustomClearanceDetail(){
$customerClearanceDetail = array(
‘DutiesPayment’ => array(
‘PaymentType’ => ‘SENDER’, // valid values RECIPIENT, SENDER and THIRD_PARTY
‘Payor’ => array(
‘ResponsibleParty’ => array(
‘AccountNumber’ => ‘510087828’,
‘Contact’ => ‘1234567890’,
‘Address’ => array(
‘CountryCode’ => ‘US’
)
)
)
),
‘DocumentContent’ => ‘NON_DOCUMENTS’,
‘CustomsValue’ => array(
‘Currency’ => ‘USD’,
‘Amount’ => 100.0
),
‘Commodities’ => array(
‘0’ => array(
‘NumberOfPieces’ => 1,
‘Description’ => ‘Books’,
‘CountryOfManufacture’ => ‘US’,
‘Weight’ => array(
‘Units’ => ‘LB’,
‘Value’ => 1.0
),
‘Quantity’ => 4,
‘QuantityUnits’ => ‘EA’,
‘UnitPrice’ => array(
‘Currency’ => ‘USD’,
‘Amount’ => 100.000000
),
‘CustomsValue’ => array(
‘Currency’ => ‘USD’,
‘Amount’ => 400.000000
)
)
),
‘ExportDetail’ => array(
‘B13AFilingOption’ => ‘NOT_REQUIRED’
)
);
return $customerClearanceDetail;
}

function addPackageLineItem1(){
$packageLineItem = array(
‘SequenceNumber’=>1,
‘GroupPackageCount’=>1,
‘Weight’ => array(
‘Value’ =>Weight2,
‘Units’ => ‘LB’
),
‘Dimensions’ => array(
‘Length’ => Length1,
‘Width’ => Width1,
‘Height’ => Height1,
‘Units’ => ‘IN’
),
‘CustomerReferences’ => array(
‘0’ => array(
‘CustomerReferenceType’ => ‘CUSTOMER_REFERENCE’,
‘Value’ => ”
), // valid values CUSTOMER_REFERENCE, INVOICE_NUMBER, P_O_NUMBER and SHIPMENT_INTEGRITY
‘1’ => array(
‘CustomerReferenceType’ => ‘INVOICE_NUMBER’,
‘Value’ => InvoiceNumber
),
‘2’ => array(
‘CustomerReferenceType’ => ‘P_O_NUMBER’,
‘Value’ => RecipientPostalCode
)
),
‘SpecialServicesRequested’ => addSpecialServices()
);
return $packageLineItem;
}
function addPackageLineItem2(){
$packageLineItem = array(
‘SequenceNumber’=>1,
‘GroupPackageCount’=>1,
‘Weight’ => array(
‘Value’ =>Weight2,
‘Units’ => ‘LB’
),
‘Dimensions’ => array(
‘Length’ => Length1,
‘Width’ => Width1,
‘Height’ => Height1,
‘Units’ => ‘IN’
),
‘CustomerReferences’ => array(
‘0’ => array(
‘CustomerReferenceType’ => ‘CUSTOMER_REFERENCE’,
‘Value’ => ”
), // valid values CUSTOMER_REFERENCE, INVOICE_NUMBER, P_O_NUMBER and SHIPMENT_INTEGRITY
‘1’ => array(
‘CustomerReferenceType’ => ‘INVOICE_NUMBER’,
‘Value’ => InvoiceNumber
),
‘2’ => array(
‘CustomerReferenceType’ => ‘P_O_NUMBER’,
‘Value’ => RecipientPostalCode
)
)//,
//’SpecialServicesRequested’ => addSpecialServices()
);
return $packageLineItem;
}
?>

Advertisements

One thought on “php code for fedex shipment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s