Inbound: retrieve a received fax using GetImageChunk

25 Nov 2016 This code sample is deprecated and in process of being removed. Check out our new code libraries and usage documentation on InterFAX @ Github.

This sample demonstrates how to retrieve an inbound (received) fax image.

You must know the fax message ID and file size, both of which are retrieved using the GetList method.

This sample uses the GetImageChunk method.


<?php

/**************** Settings begin **************/

$username  = ''; // Enter your Interfax username here
$password  = ''; // Enter your Interfax password here
$messageid = ''; // Transaction ID of the inbound message to be read
$message_size = '';  // Size in bytes of the inbound message. This value
                     // is returned by method GetList, which should be run
                     // before this one
$markasread = FALSE; // Mark the item as 'read' after downloading? If TRUE, 
                     // item will no longer be 'new' and will be shown upon
                     // subsequent list retrieval via GetList
$save_as_file = TRUE;  // If false, PDF will be shown on screen
/**************** Settings end ****************/

$chunk_size = 100000;
$image = '';

$client = new SoapClient("http://ws.interfax.net/inbound.asmx?WSDL");

$params->Username   = $username;
$params->Password   = $password;
$params->MessageID  = $messageid;
$params->MarkAsRead = $markasread;
$params->ChunkSize  = $chunk_size;

for($i=0; $i < $message_size; $i+= $chunk_size){

    $params->From = $i;
    $result = $client->GetImageChunk($params);
    if ($result->GetImageChunkResult == 0) {
        $image .= $result->Image;     // No need to base64_decode($result->Image); PHP5 does this for you

    } else {
        die('Problem retrieving image chunk #' . $i . ' on error ' . $result->GetImageChunkResult . '');
    }
}
    
// an inbound fax has been successfully retrieved
// establish file type

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

if ($save_as_file){ 

    $output_filename = $messageid . '.' . $file_type;
    
    if (!$handle = fopen($output_filename, 'w')) {
        echo "Cannot open file ($output_filename)";
        exit;
        }
    
    if (fwrite($handle, $image) === FALSE) { 
        echo "Cannot write to file $output_filename";
        exit;
        }
    
    fclose($handle);
    
} else { 
    // output to screen
    switch($file_type){
        case 'pdf':
        header ("Content-type: application/pdf"); // Set the image type according to the fax type: image/tiff or application/pdf
        break;

        case 'tif':
        header ("Content-type: image/tiff"); // Set the image type according to the fax type: image/tiff or application/pdf
        break;
    }

    echo $image;
}
       
?>

Contact us today

Talk to a member of our team about the benefits InterFAX can bring to your organization's communications processes.

Contact us today