Created
September 18, 2012 02:23
-
-
Save mattdees/3740916 to your computer and use it in GitHub Desktop.
perl module to access the WHMCS API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package API::WHMCS; | |
use strict; | |
use HTTP::Tiny; | |
use JSON::XS; | |
use Digest::MD5 ('md5_hex'); | |
our $VERSION = 0.001; | |
sub new { | |
my ( $class, %OPTS ) = @_; | |
foreach my $opt ( ( 'username', 'password', 'url' ) ) { | |
if ( !exists $OPTS{$opt} ) { | |
die "API::WHMCS::new missing \"$opt\" from parameters"; | |
} | |
} | |
my $http = HTTP::Tiny->new( 'agent' => "perl WHMCS::API/${VERSION}" ); | |
my $self = bless { | |
'username' => $OPTS{'username'}, | |
'password' => md5_hex($OPTS{'password'}), | |
'url' => $OPTS{'url'}, | |
'http' => $http, | |
}, $class; | |
return $self; | |
} | |
sub call { | |
my ( $self, %OPTS ) = @_; | |
if ( !exists $OPTS{'action'} ) { | |
die 'API::WHMCS::call missing "action" from parameters'; | |
} | |
$OPTS{'responsetype'} = 'json'; | |
$OPTS{'username'} = $self->{'username'}; | |
$OPTS{'password'} = $self->{'password'}; | |
my $res = $self->{'http'}->post_form($self->{'url'}, \%OPTS ); | |
$self->{'last'} = $res; | |
if ( $res->{'success'} ) { | |
my $decoded_res; | |
eval { | |
$decoded_res = from_json($res->{'content'} ); | |
}; | |
if ( $@ ) { | |
die "Could not decoded content from request: " . $@; | |
} | |
return $decoded_res; | |
} | |
else { | |
return $res; | |
} | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice and useful code. Would be good to have a usage example.