IP2Currency: Perl Sample Code

The sample code below provide you an example on how to obtain the latest currency exchange rate and the homeland currency by visitor's IP address.


#!/usr/bin/perl -w
use strict;
use SOAP::Lite;

# Get your own license key from http://www.fraudlabs.com
my $license = "<Enter License Key>";

# Replace these parameters to your values
my $visitorip = "68.142.197.65";
my $fromcurrencycode = "CAD";
my $tocurrencycode = "";
my $fromamount = "";

my $soap = SOAP::Lite
    -> uri('http://v1.fraudlabs.com/')
    -> proxy('http://v1.fraudlabs.com/ip2currencywebservice.asmx')
    -> on_action(sub { join "/", "http://v1.fraudlabs.com", $_[1] });

# Format the SOAP envelope            
my $method = SOAP::Data->name('IP2Currency')->attr({xmlns => 
	'http://v1.fraudlabs.com/' });

# Format the SOAP body
my @params = SOAP::Data->name('inputdata' => \SOAP::Data->value(
	SOAP::Data->name(VISITORIP=>$visitorip),
	SOAP::Data->name(FROMCURRENCYCODE=>$fromcurrencycode),
	SOAP::Data->name(TOCURRENCYCODE=>$tocurrencycode),
	SOAP::Data->name(FROMAMOUNT=>$fromamount),
	SOAP::Data->name(LICENSE=>$license)
	));

# Query the web service
my $result = $soap->call($method => @params);

unless ($result->fault) {

	# Print out the results one by one
	print "FROMCURRENCYCODE = " . $result->valueof('//FROMCURRENCYCODE') . "\n";
	print "TOCURRENCYCODE = " . $result->valueof('//TOCURRENCYCODE') . "\n";
	print "TOCURRENCYSYMBOL = " . $result->valueof('//TOCURRENCYSYMBOL') . "\n";
	print "FROMAMOUNT = " . $result->valueof('//FROMAMOUNT') . "\n";
	print "TOAMOUNT = " . $result->valueof('//TOAMOUNT') . "\n";
	print "CONVERSIONRATE = " . $result->valueof('//CONVERSIONRATE') . "\n";
	print "TOCOUNTRYNAME = " . $result->valueof('//TOCOUNTRYNAME') . "\n";
	print "TOCOUNTRYCODE = " . $result->valueof('//TOCOUNTRYCODE') . "\n";
	print "CREDITSAVAILABLE = " . $result->valueof('//CREDITSAVAILABLE') . "\n";
	print "MESSAGE = " . $result->valueof('//MESSAGE') . "\n";
} else {

	print "IP2Currency Web Service Failed!\n";
	print join ', ',
	$result->faultcode,
	$result->faultstring,
	$result->faultdetail;

}




Download the source code
You need a license key for the solution. And, it is free and available at IP2Currency Free License