Skip to content

Latest commit

 

History

History
536 lines (359 loc) · 15.7 KB

README.md

File metadata and controls

536 lines (359 loc) · 15.7 KB

InterFAX PHP Library

PHP versionBuild status

Installation | Getting Started | Contributing | Usage | License

Send and receive faxes in PHP with the InterFAX REST API.

Installation

Two versions of this library exist, 1.x and 2.x. The 1.x version provides support for older versions of PHP, whilst the 2.x branch is designed to support the more recent releases of the language:

Package Version Minimum PHP Version Maximum PHP Version
1.x 5.6 7.2
2.x 7.3 8.1

Composer

The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require interfax/interfax

with 1.x version constraint

composer require interfax/interfax:"^1"

or with 2.x version constraint

composer require interfax/interfax:"^2"

Download the release

You can download the package in its entirety (from 1.0.2 onward). The Releases page lists all stable versions. Download any file with the name interFAX-PHP-[RELEASE_NAME].zip for a package including this library and its dependencies.

Uncompress the zip file you download, and include the autoloader in your project:

require_once '/path/to/interFAX-PHP-[RELEASE_NAME]/vendor/autoload.php';

You may wish to rename the release folder to not include the RELEASE_NAME, so that you can drop in future versions without changing the include.

Build it yourself

The installation docs explain how to create a standalone installation.

Getting started

To send a fax for a pdf file:

use Interfax\Client;

$interfax = new Client(['username' => 'username', 'password' => 'password']);
$fax = $interfax->deliver(['faxNumber' => '+11111111112', 'file' => 'folder/file.pdf']);

// get the latest status:
$fax->refresh()->status; // Pending if < 0. Error if > 0

// Simple polling
while ($fax->refresh()->status < 0) {
    sleep(5);
}

Usage

Client | Account | Outbound | Inbound | Documents | Helper Classes | Query Parameters | Exceptions

Client

The client follows the 12-factor apps principle and can be either set directly or via environment variables.

$client = new Interfax\Client(['username' => '...', 'password' => '...']);

// Alternative: will utilise environment variables:
// * INTERFAX_USERNAME
// * INTERFAX_PASSWORD

$client = new Interfax\Client();

Send a Fax

To send a fax, call the deliver method on the client with the appropriate array of parameters.

$client = new Interfax\Client(['username' => '...', 'password' => '...']);
$fax = $client->deliver([
    'faxNumber' => '+442086090368',
    'file' => __DIR__ . '/../tests/Interfax/test.pdf'
]);

The deliver method will take either a file or files argument. The files is an array of file values.

Each file entry can be:

  • local path - if the file is larger than the allowed limit, it will be automatically uploaded as an Interfax\Document
  • uri (from an Interfax\Document)
  • an array defining a streamed resource (see below)
  • Interfax\File
  • Interfax\Document

Sending a stream

Because of the high degree of flexibility that PHP stream resources offer, it's not practical to retrieve information automatically from a stream to send as a fax. As such, there are certain required parameters that must be provided:

$stream = fopen('/tmp/fax.pdf', 'rb');
$fax = $client->deliver([
    'faxNumber' => '+442086090368',
    'file' => [$stream, ['name' => 'fax.pdf', 'mime_type' => 'application/pdf']]

Note that it is assumed that the stream will not exceed the file size limitation for an inline file to be sent. However, if a size parameter is provided for the stream, and this exceeds the limit, it will be automatically uploaded as a Interfax\Document

Documentation

Account

Balance

Determine the remaining faxing credits in your account.

echo $client->getBalance();
// (string) 9.86

More: documentation

Outbound

Get list | Get completed list | Get record | Get image | Cancel fax | Search | Resend fax

Interfax\Client has an outbound property that can be accessed:

$outbound = $client->outbound;

Get outbound fax list

$faxes = $client->outbound->recent();
// Interfax\Outbound\Fax[]

Options: limit, lastId, sortOrder, userId


Get completed fax list

$fax_ids = [ ... ]; // array of fax ids
$client->outbound->completed($fax_ids);
// Interfax\Outbound\Fax[]

Get outbound fax record

$client->outbound->find(fax_id)

Retrieves information regarding a previously-submitted fax, including its current status.

$fax = $client->outbound->find(123456);
//Interfax\Outbound\Fax || null

Documentation

Get outbound fax image

$client->outbound->find(fax_id)->image()

The image is retrieved via a method on the outbound fax image object.

$fax = $client->outbound->find(123456);
if ($fax) {
    $image = $fax->image();
    $image->save('path/to/save/file/to.tif');
}

Cancel an outbound fax

$client->outbound->find(fax_id)->cancel();

A fax is cancelled via a method on the Interfax\Outbound\Fax model.

$fax = $client->outbound->find(123456);
if ($fax) {
    $fax->cancel();
}

Search fax list

$client->outbound->search($options)

Search for outbound faxes with a hash array of options keyed by the accepted search parameters for the outbound search API endpoint.

$faxes = $client->outbound->search(['faxNumber' => '+1230002305555']);
// Interfax\Outbound\Fax[]

Options: ids, reference, dateFrom, dateTo, status, userId, faxNumber, limit, offset

Resend a Fax

$client->outbound->resend($id[,$newNumber])

Resend the fax identified by the given id (optionally to a new fax number).

$fax = $client->outbound->resend($id);
// Interfax\Outbound\Fax sent to the original number
$fax = $client->outbound->resend($id, $new_number);
// Interfax\Outbound\Fax sent to the $new_number

Returns a new Interfax\Outbound\Fax representing the newly created outbound fax.

Inbound

Get list | Get record | Get image | Get emails | Mark as read | Resend to email

Interfax\Client has an inbound property that supports the API endpoints for receiving faxes, as described in the Documentation

$inbound = $client->inbound;
//Interfax\Inbound

Get inbound fax list

$inbound->incoming($options = []);

Retrieves a user's list of inbound faxes.

$faxes = $inbound->incoming();
//\Interfax\Inbound\Fax[]
$faxes = $inbound->incoming(['unreadOnly' => true ...]); // see docs for list of supported query params
//\Interfax\Inbound\Fax[]

Documentation


Get inbound fax record

$inbound->find($id);

Retrieves a single fax's metadata (receive time, sender number, etc.).

$fax = $inbound->find(123456);
//\Interfax\Inbound\Fax || null

null is returned if the resource is not found. Note that this could be because the user is not permissioned for the specific fax.

Documentation


Get inbound fax image

$inbound->find($id)->image()

The image is retrieved via a method on the inbound fax object.

$fax = $client->inbound->find(123456);
if ($fax) {
    $image = $fax->image();
    $image->save('path/to/save/file/to.pdf');
}

Documentation


Get forwarding emails

$inbound->find($id)->emails()

The forwarding email details are retrieved via a method on the inbound fax object.

$fax = $client->inbound->find(123456);
if ($fax) {
    $emails = $fax->emails(); // array structure of forwarding emails.
}

The returned array is a reflection of the values returned from the emails endpoint of the REST API:

[
    [
       'emailAddress' => '[email protected]',
       'messageStatus' => 0,
       'completionTime' => '2012-0623T17 => 24 => 11'
    ],
    //...
];

Documentation


Mark as read/unread

$inbound->find($id)->markRead(); $inbound->find($id)->markUnread();

The inbound fax object provides methods to change the read status.

$fax = $client->inbound->find(123456);
if ($fax) {
    $fax->markUnread(); // returns true
    $fax->markRead(); //returns true
}

Documentation


Resend inbound fax

$inbound->find($id)->resend();

The resend method is on the inbound fax object.

$fax = $client->inbound->find(123456);
if ($fax) {
    $fax->resend(); 
}

Documents

Create | Upload chunk | Properties | Cancel

The Interfax\Document class allows for the uploading of larger files for faxing. The following is an example of how one should be created:

$document = $client->documents->create('test.pdf', filesize('test.pdf'));
$stream = fopen('test.pdf', 'rb');
$current = 0;
while (!feof($stream)) {
    $chunk = fread($stream, 500);
    $end = $current + strlen($chunk);
    $doc->upload($current, $end-1, $chunk);
    $current = $end;
}
fclose($stream);

Create document

$params = [...]; // see documentation for possible params
$document = $client->documents->create($filename, filesize($filename), $params);
// Interfax\Document

Documentation

Upload chunk

$document->upload($start, $end, $data); // returns the document object.

Note no verification of data takes place - an exception wil be raised if values do not match appropriately.

Document properties

As per the documentation a Document has a number of properties which are accessible:

$document->status;
$document->fileName;
$document->refresh()->attributes();
$document->location;
// or as returned by the API:
$document->uri;

Cancel document

$document->cancel(); //returns the $document instance

Can be done prior to completion or afterward

Helper Classes

Outbound Fax

The Interfax\Outbound\Fax class wraps the details of any fax sent, and is returned by most of the Outbound methods.

It offers several methods to manage or retrieve information about the fax.

// fluent methods that return the $fax instance
$fax->refresh(); // refreshes the data on the fax object
$fax->cancel(); // cancel the fax, returns true on success
$fax->hide(); // hides the faxes from the fax lists

$image = $fax->image(); // returns Interfax\Image
$new_fax = $fax->resend('+1111111'); // returns a new Interfax\Outbound\Fax
$fax->attributes(); // hash array of fax data properties - see details below

Outbound fax properties

Properties on the Fax vary depending on which method call has been used to create the instance. Requesting a property that has not been received will raise a SPL \OutOfBoundsException

Documentation

These are all accessible on a fax instance:

echo $fax->completionTime
echo $fax->duration
...

Note values will all be returned as strings.

For convenience, a hash array of the properties can be retrieved

$fax->attributes();

Status should always be available. The values of the status codes are Documented here

Inbound Fax

The incoming equivalent of the outbound fax class, the Interfax\Inbound\Fax class wraps the details of any incoming fax, and is returned by the Interfax\Inbound methods where appropriate.

// fluent methods that return the $fax instance for method chaining
$fax->refresh(); // reload properties of the inbound fax
$fax->markRead(); // mark the fax read - returns true or throws exception
$fax->markUnread(); // mark the fax unread - returns true or throws exception
$fax->resend();

$image = $fax->image(); // Returns a Interfax\Image for this fax
$email_array = $fax->emails(); // see below for details on the structure of this array
$fax->attributes(); // hash array of properties

Query parameters

Where methods support a hash array structure of query parameters, these will be passed through to the API endpoint as provided. This ensures that any future parameters that might be added will be supported by the API as is.

The only values that are manipulated are booleans, which will be translated to the text 'TRUE' and 'FALSE' as appropriate.

Documentation

Exceptions

Any method call that involves a call to the Interfax RESTful API may throw an instance of Interfax\Exception\RequestException.

An exception is thrown for any requests that do not return a successful HTTP Status code. The goal of this Exception is to provide a convenience wrapper around information that may have been returned.

Certain responses from the API will provide more detail, and where this occurs, it will be appended to the message of the Exception.

try {
    $interfax->deliver(...);
} catch (Interfax\Exception\RequestException $e) {
    echo $e->getMessage();
    // contains text detail that is available
    echo $e->getStatusCode();
    // the http status code that was received
    throw $e->getWrappedException();
    // The underlying Guzzle exception that was caught by the Interfax Client.
}

Contributing

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. Submit a Pull request so that we can review your changes

Which version of the library?

Functionality for the API in both versions of the library should be the same, and its therefore intended that the core code continues to be the same in both versions. The differences primarily exist to enable continuity of test coverage as PHP evolves (and phpunit evolves with it)

As such, the core functionality should be developed in a way that will work with PHP 5.6 and up, and be consistent in both versions. Usage of newer language features in PHP will not be accepted in the core code.

Running tests

Ensure that composer is installed, then run the following commands.

composer install
./vendor/bin/phpunit

License

This library is released under the MIT License.