***** infoCopter.com/perl *****

XML RPC Samples



up
#!/usr/bin/perl -w
use strict;
$| = 1;

use RPC::XML::Client;
require "xmlrpcutil_inc.pl";

&main();

#########################
sub main() {
#########################
	my $cli = RPC::XML::Client->new('https://reto.provisioning.extlab.sunrise.ch/OpenLDAP_iso.php');

	my $request;
	$request = RPC::XML::request->new('LookupCustomer',
		RPC::XML::struct->new(
			'mode'   => RPC::XML::string->new("interactive"),
			'source' => RPC::XML::string->new("openLDAP"),
			'lookup' => RPC::XML::string->new('AdminProfile'),
			'cn'     => RPC::XML::string->new('foo@example.org'),
		)
	);

	my $response = $cli->simple_request($request);


	if (!$response) {
		print "$RPC::XML::ERROR \n"; # No response
	}
	else {
		print "SunriseStatusCode: ",
			 $response->{'SunriseStatusCode'}, "\n"; # 001000... ist okay
		my $hash_ref = &prepareResponse($response);
		foreach (sort keys %$hash_ref) {
			print "- $_ \t= [\"", $hash_ref->{$_}, "\"]\n";
		}
	}
}


xmlrpcutil_inc.pl

my $response;

#######################################################
sub prepareResponse ($) {
#######################################################
	$response = $_[0];
	my %hash = ();
	foreach my $key (sort keys %$response) {
		my $dumpArr = 0;
		my @array = $response->{$key};
		foreach (@array) {
			if (ref $_ eq 'ARRAY') {
				foreach (@{$_}) {
					if (ref $_ eq 'ARRAY') {
						my $arr_ref = &dumpArray($key);
						$dumpArr = 1;
						my $i = 0;
						foreach (@{$arr_ref}) {
							$hash{"$key\.$i"} = $_;
							$i++;
						}
					}
					else {
						$hash{$key} = $_;
					}
				}
			}
			else {
				$hash{$key} = $_;
			}
		}
	}
	\%hash;
}

sub dumpArray ($) {
	# print "Dumping Array \"$_[0]\"\n";
	my @array = ();
	foreach (@{$response->{$_[0]}}) {
		foreach (@{$_}) {
			push(@array, $_);
		}
	}
	\@array;
}

1;
© reto :)