#!/usr/bin/perl

use XML::Simple;
use CGI::Carp;
use Data::Dumper;

   # Required Variables
#   $rateurl = "https://gatewaybeta.fedex.com:443/web-services/rate";		# The URL of the XML server
   $rateurl = "https://wsbeta.fedex.com/web-services/rate";
#   $rateurl = "https://gatewaybeta.fedex.com/xml/rate";

#$rate_args{'account'}  = '186274725'; #FedEx Account Number # Fedex account number 186274725
#$rate_args{'meter'}    = '102310577';  #FedEx Meter Number 
#$rate_args{'key'}      = 'su0IbLn83awfxRvh';
#$rate_args{'password'} = 'KUoBkBzjG1pbaajq1z6RsMsWF'; # ECQfM9BQm7uxiqTI4kIauuaRh
#$rate_args{'uri'}      = 'https://gateway.fedex.com:443/xml';


$line = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://fedex.com/ws/rate/v9">
<SOAP-ENV:Body>
<RateRequest>
<WebAuthenticationDetail>
<UserCredential>
<Key>su0IbLn83awfxRvh</Key>
<Password>KUoBkBzjG1pbaajq1z6RsMsWF</Password>
</UserCredential>
</WebAuthenticationDetail>
<ClientDetail>
<AccountNumber>186274725</AccountNumber>
<MeterNumber>102310577</MeterNumber>
</ClientDetail>
<TransactionDetail>
<CustomerTransactionId>Reformatted</CustomerTransactionId>
</TransactionDetail>
<Version>
<ServiceId>crs</ServiceId>
<Major>9</Major>
<Intermediate>0</Intermediate>
<Minor>0</Minor>
</Version>
<RequestedShipment>
<ShipTimestamp>2011-01-29T09:30:47-05:00</ShipTimestamp>
<DropoffType>REGULAR_PICKUP</DropoffType>
<ServiceType>FIRST_OVERNIGHT</ServiceType>
<PackagingType>YOUR_PACKAGING</PackagingType>
<TotalWeight>
<Units>LB</Units>
<Value>50.0</Value>
</TotalWeight>
<Shipper>
<AccountNumber/>
<Contact>
<CompanyName>FedEx-WAPI</CompanyName>
<PhoneNumber>1234567890</PhoneNumber>
</Contact>
<Address>
<StreetLines>SN2000 Test Meter 8</StreetLines>
<StreetLines>10 Fedex Parkway</StreetLines>
<City>Texas</City>
<StateOrProvinceCode>TX</StateOrProvinceCode>
<PostalCode>73301</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</Shipper>
<Recipient>
<AccountNumber/>
<Contact>
<PersonName>Recipient Contact</PersonName>
<PhoneNumber>1234567890</PhoneNumber>
</Contact>
<Address>
<StreetLines>Recipient Address Line 1</StreetLines>
<StreetLines>Recipient Address Line 2</StreetLines>
<City>Minneapolis</City>
<StateOrProvinceCode>MN</StateOrProvinceCode>
<PostalCode>55411</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</Recipient>
<ShippingChargesPayment>
<PaymentType>SENDER</PaymentType>
<Payor>
<AccountNumber/>
<CountryCode>USD</CountryCode>
</Payor>
</ShippingChargesPayment>
<LabelSpecification>
<LabelFormatType>COMMON2D</LabelFormatType>
<ImageType>PNG</ImageType>
<LabelStockType>PAPER_4X6</LabelStockType>
</LabelSpecification>
<RateRequestTypes>ACCOUNT</RateRequestTypes>
<PackageCount>1</PackageCount>
<PackageDetail>INDIVIDUAL_PACKAGES</PackageDetail>
<RequestedPackageLineItems>
<SequenceNumber>1</SequenceNumber>
<Weight>
<Units>LB</Units>
<Value>20.0</Value>
</Weight>
<Dimensions>
<Length>12</Length>
<Width>12</Width>
<Height>12</Height>
<Units>IN</Units>
</Dimensions>
<ContentRecords>
<PartNumber>123445</PartNumber>
<ItemNumber>kjdjalsro1262739827</ItemNumber>
<ReceivedQuantity>12</ReceivedQuantity>
<Description>ContentDescription</Description>
</ContentRecords>
</RequestedPackageLineItems>
</RequestedShipment>
</RateRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

   use LWP::UserAgent;					# See LWP's docs if you're curious about this part.
   $ua = LWP::UserAgent->new;				# man LWP   or   perldoc LWP    or
   $ua->agent('Myspyder');					# http://search.cpan.org/doc/GAAS/libwww-perl-5.64/lib/LWP.pm

   # Create the request object
      $req = HTTP::Request->new(POST => "$rateurl");
      $req->content_type('application/soap+xml; charset=utf-8');
      $req->content($line);
# $req->header('If-SSL-Cert-Subject'=>'/CN=wsbeta.fedex.com');		# This is neat, it makes it fail if the SSL doesn't match

   # Pass request to the user agent and get a response back
   my $res = $ua->request($req);

   # Return the response if it worked.
   if ($res->is_success) { $reply = $res->content; }

   # Otherwise, put a warning in the log file, and return nothing.
   warn("Warning: \"" . $res->status_line . "\" URL: $url Parms: $parms Method: $method Useragent: $useragent");

   	my $xml_obj  = new XML::Simple;    
    my $data = $xml_obj->XMLin($reply); # Time consuming operation. could use a regexp to speed up if necessary. 
    
	print Dumper $data; 
	
   
    #print "$reply\n";


