Skip to content

Instantly share code, notes, and snippets.

@mattdees
Created September 18, 2012 02:23
Show Gist options
  • Save mattdees/3740916 to your computer and use it in GitHub Desktop.
Save mattdees/3740916 to your computer and use it in GitHub Desktop.
perl module to access the WHMCS API
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;
@sfolayan
Copy link

Nice and useful code. Would be good to have a usage example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment