PHP4: View the status of a fax

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.

Example of how to view the status of a fax with PHP4

With thanks to Justin Horn from Justechnology, LLC


<?
/**
* Get Fax Status using PHP/PEAR::SOAP Example:
* - Using FaxStatus() Method from InterFax Web Services
* - In this example we are getting status information for one
* specific transaction
*
* NOTE:
* PEAR::SOAP is still in the beta stages of development
* The FaxStatus() method returned results are a bit more complex to
* deal with than some of the other InterFax methods and caused a few
* inconsistencies. I patched the problem by viewing the var_dump()
* of different results of FaxStatus() and based on that created an
* appropriate function to parse it independent of the
* inconsistencies in the return value
* + See parseFaxStatusResult() below for more information
*/


/**
* parseFaxStatusResult
*
* Extract the data from the FaxStatus Result and return an array with
* the appropriate values. Also, takes the 3 output parameters and
* returns the appropriate values
*
* This function will work for ANY FaxStatus Result and will always
* return the same, independent of the number of transactions returned
*
* @param FaxStatus returned result
* int &TotalCount
* int &ListSize
* int &ResultCode
* @return mixed returns Array of FaxItems or String with an error message
*/
function parseFaxStatusResult($result, &$TotalCount, &$ListSize, &$ResultCode)
{
// Copy the output parameters from $result into the appropriate variable
$TotalCount = $result['TotalCount'];
$ListSize = $result['ListSize'];
$ResultCode = $result['ResultCode'];

// Check if InterFax Method executed properly
if($result['ResultCode'] != 0)
{
return false;
}

if($result['ListSize'] == 1)
{
/**
* If only one transaction is returned then you access the $result
* by result['FaxStatusResult']->FaxItem->____FaxItemIndex____
*/
$parsedResult[0]['TransactionID']=$result['FaxStatusResult']->FaxItem->TransactionID;
$parsedResult[0]['SubmitTime']=$result['FaxStatusResult']->FaxItem->SubmitTime;
$parsedResult[0]['PostponeTime']=$result['FaxStatusResult']->FaxItem->PostponeTime;
$parsedResult[0]['CompletionTime']=$result['FaxStatusResult']->FaxItem->CompletionTime;
$parsedResult[0]['DestinationFax']=$result['FaxStatusResult']->FaxItem->DestinationFax;
$parsedResult[0]['RemoteCSID']=$result['FaxStatusResult']->FaxItem->RemoteCSID;
$parsedResult[0]['PagesSent']=$result['FaxStatusResult']->FaxItem->PagesSent;
$parsedResult[0]['Status']=$result['FaxStatusResult']->FaxItem->Status;
$parsedResult[0]['Duration']=$result['FaxStatusResult']->FaxItem->Duration;
$parsedResult[0]['Subject']=$result['FaxStatusResult']->FaxItem->Subject;
$parsedResult[0]['PagesSubmitted']=$result['FaxStatusResult']->FaxItem->PagesSubmitted;

return $parsedResult;
}
else if($result['ListSize'] > 1)
{
/**
* If more than one transaction is returned then you access the
* $result by result['FaxStatusResult'][index]->____FaxItemIndex____
*/
for($i=0; $i<$result['ListSize']; $i++)
{
$parsedResult[$i]['TransactionID']=$result['FaxStatusResult'][$i]->TransactionID;
$parsedResult[$i]['SubmitTime']=$result['FaxStatusResult'][$i]->SubmitTime;
$parsedResult[$i]['PostponeTime']=$result['FaxStatusResult'][$i]->PostponeTime;
$parsedResult[$i]['CompletionTime']=$result['FaxStatusResult'][$i]->CompletionTime;
$parsedResult[$i]['DestinationFax']=$result['FaxStatusResult'][$i]->DestinationFax;
$parsedResult[$i]['RemoteCSID']=$result['FaxStatusResult'][$i]->RemoteCSID;
$parsedResult[$i]['PagesSent']=$result['FaxStatusResult'][$i]->PagesSent;
$parsedResult[$i]['Status']=$result['FaxStatusResult'][$i]->Status;
$parsedResult[$i]['Duration']=$result['FaxStatusResult'][$i]->Duration;
$parsedResult[$i]['Subject']=$result['FaxStatusResult'][$i]->Subject;
$parsedResult[$i]['PagesSubmitted']=$result['FaxStatusResult'][$i]->PagesSubmitted;
}
return $parsedResult;
}
else
{
/**
* If no results were returned, then set ResultCode to custom error
* number [Replaces the '0' {good transaction} because there
* is an error, No Data was found] and return false
*/
$ResultCode = -1; // -1 == No Data
return false;
}
}

/**
* PEAR::SOAP class [https://pear.php.net/package/SOAP]
*/
require_once 'SOAP/Client.php';

// URL path to InterFax Web Services (using https for security purposes)
$wsdl_url = 'https://ws.interfax.net/dfs.asmx?wsdl';

// Create an instance of the SOAP_WSDL class
$WSDL = new SOAP_WSDL($wsdl_url);

/**
* This function creates a proxy to the SOAP services so that you can
* access the InterFax methods directly as if you were calling a local
* class function
*/
$client = $WSDL->getProxy();

/**
* Based on the way the FaxStatus function works, we need to pass the
* transaction id we want plus 1 and MaxItems equal to 1
*
* NOTE:
* To get multiple transactions the ONLY things you need to change
* in this script are:
* - Change $transId value to the max transaction you want info on
* (for all use 999999999)
* - Change $maxItems value to the total number you want to receive
*/
$transId = [Transaction number you want to view];
$transId++;
$maxItems=1;

/**
* Invoke the InterFax FaxStatus Method
* - Passing the Output parameters (TotalCount, ListSize,
* and ResultCode) by reference was causing errors do to limitation
* of PHP and PEAR::SOAP. To get around this problem just enter 0
* to fill these arguments
* - The returned result from FaxStatus is an array with the following
* 4 indexs: FaxStatusResult, TotalCount, ListSize, ResultCode
* To extract the data properly from this returned result we will
* use the custom function parseFaxStatusResult
*/
$result = $client->FaxStatus('your username', 'your password', $transId, $maxItems, 0, 0, 0);

// Check to make sure the PEAR SOAP client ran properly
if(PEAR::isError($result))
{
print("PEAR Error: " . $result->getMessage() . "\n");
exit;
}

// Call parseFaxStatusResult function to extract data
$parsedResult = parseFaxStatusResult($result, &$TotalCount, &$ListSize, &$ResultCode);

// Check to make sure there were no error codes returned
if(!$parsedResult)
{
echo "Error: $ResultCode";
exit;
}

/**
* Print out the results of FaxStatus
*/
echo "Viewing $ListSize of $TotalCount fax transactions:";
for($i=0; $i<$ListSize; $i++)
{
echo "TransactionID: " . $parsedResult[$i]['TransactionID'] . " - ";
echo "Status: " . $parsedResult[$i]['Status'] . "";
}
?>