Perl Fax – Use GetImageChunk to Retrieve Inbound Fax Image

Download the Perl fax code samples.

In this Perl fax sample, GetImageChunk is used to retrieve a fax image in multiple chunks (the chunk size is configurable).

In order to retrieve the Perl fax image, you need the fax message ID, which is retrieved using the GetList method.

The following Perl fax script uses GetImageChunk to retrieve an image for an inbound (received) fax:


#/**************** Settings begin **************/
my $username  = ''; # Enter your Interfax username here
my $password  = ''; # Enter your Interfax password here
# Size in bytes of the inbound message.
my $messageSize = 0;
my $messageID = "";
my $markAsRead = "false";
my $chunkSize = 100000;
my $NS = 'https://www.interfax.net';
#/**************** Settings end ****************/


my $file_type;
my $result;
my $image = "";
my $client = SOAP::Lite
    ->uri($NS)
    ->on_action( sub { join '/', $NS, $_[1] } ) 
    ->proxy('https://ws.interfax.net/inbound.asmx?wsdl');
    
for(my $i=0; $i call(SOAP::Data->name('GetImageChunk')->attr({xmlns => $NS}) =>
            SOAP::Data->name('Username')->value($username)->type(''),
            SOAP::Data->name('Password')->value($password)->type(''),
            SOAP::Data->name('MessageID')->value($messageID)->type(''),
            SOAP::Data->name('MarkAsRead')->value($markAsRead)->type(''),
            SOAP::Data->name('ChunkSize')->value($chunkSize)->type(''),
            SOAP::Data->name('From')->value($i)->type('')
    );
    
    if ( $result->fault ) {
        print $result->faultstring . "\n";
    } else {
        if( $result->valueof('//GetImageChunkResult') == 0 ) {
             $image .= decode_base64( $result->valueof('//Image') );
        } else {
            print "Error retrieving image chunk #" . $i . ", return code=" . $result->valueof('//GetImageChunkResult') . "\n";
        }
    }
}


if( $image ne '') {
    if( substr($image, 0, 3) eq 'II*' ){
        # TIFF file
        $file_type = 'tif';
    } else {
        if( substr($image, 0, 4) eq '%PDF' ) {
            # PDF
            $file_type = 'pdf';
        } else {
            die('Unrecognized file type');
        }
    }

    open (FILE, ">>$messageID.$file_type");
    print FILE $image;
    close (FILE);   
    
    print "File saved" . "\n";
}