PHP eFax
|
The PHP script to send and receive faxes over the Internet. More...
Public Member Functions | |
eFax ($use_pear_http_request=true) | |
Initializes the eFax object. More... | |
send ($msg) | |
Send the message to eFax. More... | |
parse_disposition ($msg) | |
Function used to parse the disposition. More... | |
parse_inbound_message ($msg) | |
Parse an Inbound Fax. More... | |
message () | |
Create the XML message. More... | |
set_outbound_url ($url) | |
Set the URL used to send the fax. More... | |
set_account_id ($account_id) | |
Set the account identifier. More... | |
set_raw_account_id ($account_id) | |
Set the account identifier. More... | |
set_user_name ($user_name) | |
Set the user name. More... | |
set_user_password ($user_password) | |
Set the user password. More... | |
set_fax_id ($fax_id) | |
Set this fax request identifier. More... | |
set_duplicate_id ($duplicate_id) | |
Set whether fax identifiers can be duplicated. More... | |
set_resolution ($resolution) | |
Set the resolution used for transmission. More... | |
set_priority ($priority) | |
Set the priority for this transmission. More... | |
set_self_busy ($self_busy) | |
Queue faxes sent to the same number. More... | |
set_fax_header ($fax_header) | |
Defines the fax header. More... | |
set_disposition_url ($disposition_url) | |
Defines the return URL to signal disposition of the request. More... | |
set_disposition_level ($disposition_level) | |
Defines the type of reply. More... | |
set_disposition_method ($method) | |
Defines the method to use to send the reply. More... | |
set_disposition_language ($language) | |
Defines the disposition language. More... | |
add_disposition_email ($name, $email) | |
Add an email address for the response. More... | |
add_recipient ($name, $company, $fax) | |
Add a recipient name, company and fax number. More... | |
add_file ($type, $contents) | |
Add a file: the content of the fax. More... | |
get_response () | |
The response of the last eFax::send() command. More... | |
get_status () | |
Get the status of the last call to eFax::send() . More... | |
get_docid () | |
Return the document identifier after a eFax::send() . More... | |
get_error_level () | |
Get the last error level. More... | |
get_error_description () | |
Get the last error description. More... | |
get_result_fax_id () | |
Return the fax identifier. More... | |
get_result_docid () | |
Return the document identifier from a disposition or inbound request. More... | |
get_result_fax_number () | |
Return the fax number at the other end. More... | |
get_result_completion_date () | |
Return the date and time when the transmission ended. More... | |
get_result_fax_status () | |
Return the disposition or fax request status. More... | |
get_result_csid () | |
Return the recipient or sender fax machine identification. More... | |
get_result_duration () | |
Return the duration of the fax transmission. More... | |
get_result_pages () | |
Return the number of pages sent. More... | |
get_result_retries () | |
Return the number of times eFax had to dial to process the fax. More... | |
get_result_error_message ($status=null) | |
Return the error message from a status code. More... | |
get_result_error_class ($status=null) | |
Return the error class from a status code. More... | |
get_result_request_date () | |
Get the date and time when the Inbound fax was received. More... | |
get_result_request_type () | |
Get the type of inbound fax request. More... | |
get_result_fax_name () | |
Retrieve the name of the fax received. More... | |
get_result_user_fields () | |
Retrieve the array of user fields. More... | |
get_result_barcodes () | |
Retrieve the array of barcode keys. More... | |
get_result_files () | |
Retrieve the fax pages. More... | |
Public Attributes | |
const | RESPOND_SUCCESS = 1 |
Received when sending the fax is a success. | |
const | RESPOND_ERROR = 2 |
Received when sending the fax is a failure. | |
This class is based on the developer documents provided by eFax (http://www.efax.com).
It will generate an XML file that is 100% compatible with the eFax specification. It supports all the data types and all the different modes available in the API.
If you know how much it costs to send one page, and you receive the disposition, you will be able to compute exactly how much sending the fax did cost:
Before calling eFax::send()
you will need to call eFax::message()
. Before calling eFax::message()
you will need to at least call the following functions to setup your eFax object:
eFax::add_disposition_email | ( | $name, | |
) |
This function adds one email address where the response will be sent. As many email addresses as you want can be included.
The default is no email addresses. However, your eFax setup may include an email address that is used by default whenever the disposition method is set to EMAIL.
When you add an email address, the recipient name is optional. This function is not capable of separating the recipient name from the email address. You must do that before calling this function.
The request identifier is saved in:
eFaxException | is thrown whenever the parameters are not null or strings. |
[in] | $name | The name of the recipient (can be null) |
[in] | The email of the recipient (cannot be null) |
eFax::add_file | ( | $type, | |
$contents | |||
) |
This function adds one file to the content of the fax. There can be as many files as necessary to print out your entire fax. Each file can have a different format. The format must be correct for the data or the fax will fail.
The data parameter is the actual content of the file. This function will NOT read the content of a file on disk or otherwise. The function will encode the content for you (base64). You can use the file_get_contents() function to read a file from disk and pass it to this function as a string.
Supported file formats:
eFaxException | is thrown if the file type is not one supported by eFax. Note that the names can be either all lowercase or all uppercase. |
[in] | $type | One of: doc, xls, tif, pdf, txt, html, htm or rtf. |
[in] | $contents | The contents of the file (NO file is read from disk!) |
eFax::add_recipient | ( | $name, | |
$company, | |||
$fax | |||
) |
This function adds one recipient to the list of recipients of this fax. The name and company name of the recipient are optional. The fax number is mandatory.
International fax numbers are dialed from the USA and must start with 011 and the country code character. The format of the fax phone number is "[-+ ()0-9]{5,25}". All characters other than digits are ignored. At least 5 characters and at most 25 are necessary.
The request identifier is saved in:
eFaxException | is thrown whenever the parameters are not null, numeric or strings, a name is more than 50 characters or the fax number is less than 5 or more than 25 characters. |
[in] | $name | The name of the fax recipient (can be null) |
[in] | $company | The name of the fax recipient company (can be null) |
[in] | $fax | The fax number where the fax is being sent (cannot be null) |
eFax::eFax | ( | $use_pear_http_request = true | ) |
By default, the output URL is the eFax URL expected by the eFax Developers. Namely, this is:
https://secure.efaxdeveloper.com/EFax_WebFax.serv
The outbound encoding is set to application/x-www-form-urlencoded.
The fax resolution is set to STANDARD.
The disposition level is set to both: success and error.
The status is set to 0 as if a response indicated a failure.
[in] | $use_pear_http_request | If true (the default) use the PEAR HttpRequest object (requires http.so), otherwise use the http_request.php class. |
References RESPOND_ERROR, and RESPOND_SUCCESS.
eFax::get_docid | ( | ) |
This function returns the document identifier found in the outbound response message. This can be reused later to request status information and when the final disposition is sent to us.
eFax::get_error_description | ( | ) |
This function returns the description of the last error that occurred after a request was sent to eFax. This is a string of undefined length. It is intended for display and will be in English.
eFax::get_error_level | ( | ) |
This function returns the level of the last error that occurred after a request was sent to eFax.
eFax::get_response | ( | ) |
This function returns the raw response of the last eFax::send()
command.
eFax::send()
does not send anything, then the response will be "not sent" when it happens before the HTTP POST or the empty string if no reply is received from the eFax::send()
.eFax::send()
eFax::get_result_barcodes | ( | ) |
This function retrieves the array of barcode objects as found in the inbound message.
The array is not keyed (i.e. uses numeric indices). The order of the barcode objects in the array should be the same as the order in which they were found in the source message. However, it is strongly advised that you use the page and sequence information to know for sure where a barcode is from.
eFaxException | is thrown whenever this function is called before a successful call to the eFax::parse_inbound_message() . |
eFax::get_result_completion_date | ( | ) |
This function returns a Unix timestamp with the date and time when the transmission of the fax ended. The timestamp is defined in UTC.
The eFax::parse_inbound_message()
may or may not return a completion date field. If so, this function returns -1.
eFaxException | is raised when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_csid | ( | ) |
This function returns some identification of the recipient (in a disposition) or the sender (in a fax request) as defined by the destination fax machine. This is not always available since many fax machines do not support this feature.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_docid | ( | ) |
This function returns the document identifier found in the disposition message or inbound request.
This identifier is automatically assigned by eFax to every single fax sent via eFax. This is the same as the identifier returned in the outbound response when you sent a fax.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_duration | ( | ) |
This function returns the number of seconds that the fax transmission took. The time is rounded up to the closest second (i.e. if the time represents 0.1 second, then 1 is returned.)
eFaxException | when you call this function before a successful call to eFax::parse_disposition() . |
eFax::get_result_error_class | ( | $status = null | ) |
This function converts a status code in an error class. The status code can be set to null in which case the result status code is used.
If the status code is not known, then the function returns the special class "?".
When there was no error, the special class " " is returned (a space.)
[in] | $status | The status code to convert or null |
$status
eFax::get_result_error_message | ( | $status = null | ) |
This function converts a status code in an error message in English. The input status code parameter can be set to null in which case the internal result status code is used from the last call to the eFax::parse_disposition()
function.
If the status code is not known, then the function returns the special error "unknown status code".
If the function is called with null and no disposition was parsed yet, the function returns "no disposition message has been parsed".
[in] | $status | The status code to convert or null |
$status
codeeFax::get_result_fax_id | ( | ) |
This function returns the fax identifier found in the disposition message or the fax request message. It is extracted from the message with a call to eFax::parse_disposition()
or eFax::parse_inbound_message()
.
The disposition has a fax identifier only if you called the eFax::set_fax_id()
before sending a fax with eFax::send()
. This is that identifier that you probably want to make unique.
The fax request message sets the fax identifier to your account identifier (InboundPostRequest/FaxControl/AccountID). If you are handling multiple eFax accounts, it can be quite handy to check this identifier to know which number was used.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_fax_name | ( | ) |
This function returns the name of the of fax as defined in the eFax request.
eFaxException | is raised if the eFax::parse_inbound_message() function was not called before hand. |
eFax::get_result_fax_number | ( | ) |
This function returns the fax number at the other end. It may be found in the disposition message or the inbound fax request.
Note that you will receive one disposition per fax number defined in the outbound request and each one will have the same identifiers (fax identifier and document identifier.) Thus, if you need to distinguish each disposition, it is necessary to also check the fax number. (Although it may have been blocked and thus be undefined.)
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_fax_status | ( | ) |
This function returns the status of the fax transmission from the message parsed with a call to eFax::parse_disposition()
or eFax::parse_inbound_message()
.
The status is zero (0) when the fax was successfully sent or received. The status is an error number otherwise. Use the eFax::get_result_error_message()
function to retrieve the corresponding error string.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_files | ( | ) |
This function returns a list of the pages received in an inbound fax or those added with the eFax::add_file()
function call. This includes the content, the type of the data and the page number when available.
The data comes from the $files
variable member. It is an array of arrays The following sample of code shows how one can save the data in a set of files.
The $file
array includes a set of arrays, possibly none. These sub-arrays have two or three indices defined as:
$files
array.eFax::get_result_pages | ( | ) |
This function returns the number of pages that have been sent. If the fax transmission failed, this number indicates the number of pages that have successfully been sent.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() or eFax::parse_inbound_message() . |
eFax::get_result_request_date | ( | ) |
This function returns the date and time in the form of a Unix timestamp of when the XML request was sent from eFax Developer.
If you want to know when the fax was received, use the eFax::get_result_completion_date()
function instead.
eFax::parse_inbound_message()
.eFaxException | is raised if the eFax::parse_inbound_message() function was not called before hand. |
eFax::get_result_request_type | ( | ) |
The eFax Developer system can send a fax on receipt (New Inbound) and it can resend a fax that it received earlier (Manual Repost).
This function let you check the type of fax you are receiving.
eFaxException | is raised if the eFax::parse_inbound_message() function was not called before hand. |
eFax::get_result_retries | ( | ) |
This function returns the number of times eFax dial the fax number in order to transmit the documents. A first attempt success is represented by a 1 here.
eFaxException | when you call this function before a successful call to eFax::parse_disposition() . |
eFax::get_result_user_fields | ( | ) |
This function returns the array of user fields as found in the inbound message.
The keys used for the array are the field names and the content of each entry is the field value. You can parse the entire array with the following code:
eFaxException | is raised if this function is called before the eFax::parse_inbound_message() function. |
eFax::get_status | ( | ) |
This function returns the status that the last call to eFax::send()
generated. By default, the status is set to 0 meaning that eFax::send()
was never called. Once eFax::send()
returns with no exception, then status will be set to either 1 (success) or 2 (failure).
When the status is 2, the eFax::get_error_level()
and eFax::get_error_description()
functions can be used to retrieve more information about the error.
eFax::send()
command status on exit. eFax::message | ( | ) |
This function generates the XML message that is to be sent to eFax.
References RESPOND_ERROR, and RESPOND_SUCCESS.
eFax::parse_disposition | ( | $msg | ) |
This function transforms the disposition XML message into a set of internal variables that can then be queried using get functions.
The disposition includes the user name and password. These will be checked against the user name and password defined in this eFax object. If there is a mismatch, then the function returns false and stops.
Please, see the Parsing the fax disposition section for an example of handling a disposition.
At this time, it is necessary to use stripslashes()
before one can pass the XML data to this parse function.
Note that the "Post Successful" reply can be sent at any time. You have to judge where is the best position for the message in your code. It is likely a good idea to do it after you successfully saved the information about the fax disposition, but eFax does not really send you the data again either way. (It will email you about the failure though.)
eFaxException | is raised if the XML message is not a valid outbound disposition file or the user and password where not specified. |
[in] | $msg | The message to parse |
eFax::parse_inbound_message | ( | $msg | ) |
This function is called whenever you receive an Inbound Fax from eFax Developer. It will send the necessary "Post Successful" response if the header of the message is parsed successfully. This means the login, password, and date of the message were valid.
The message includes the user name and password defined in the Inbound Settings section. These will be checked against the user name and password defined in this eFax object with the set_user_name() and set_user_password() functions. If there is a mismatch, then the function returns false and stops.
If the function returns true, then the data defined in the XML file is now available for you to query from your eFax object.
The message is expected to be received via a POST and will be in the $_POST
['xml'] variable. Yet, the data will include backslashes and you must clean it before calling the parse function as in:
eFaxException | is raised if the XML message is not a valid inbound request file. The minimum requirements checked are the following valid tags: InboundPostRequest (root tag), AccessControl (to ensure user), UserName (the login name), Password (your password), and FaxControl (the actual fax data, although all the sub-tags are not required.) The other tags are not considered so critical and as such only generate an error when missing. |
[in] | $msg | The raw XML data sent to you by eFax Developer via a POST request. |
eFax::send | ( | $msg | ) |
This function sends the specified message to eFax. The message can be generated using the eFax::message()
function (actually, it is strongly recommended that you do so unless you have another tool to generate such request reliably.)
IMPORTANT NOTE: You need to use the HTTPS protocol and for that you must have an HTTPS website with a valid certificate. Although just sending a fax does not require an HTTPS server, you will need to support connecting to an HTTPS server. And if you want to handle the disposition via your Web Server (instead of just getting an email), then you do not have a choice. You must have a valid SSL certificate.
Also, as mentioned at the end of a successful compile, add
in your php.ini file.
The phpize function is available in php5-dev (apt-get install php5-dev). You may also need the curl library development environment. Get the libcurl3-openssl-dev, for instance.
eFaxException | is thrown if some parameter is invalid in the input or the response data. If somehow the transmission fails, the function returns false instead. However, the HttpRequest may throw some exception that will not be caught here. |
[in] | $msg | The fax message (i.e. the XML message to forward.) |
eFax::set_account_id | ( | $account_id | ) |
This function is used to set the eFax account identifier. The identifier is your eFax Developer fax number.
The account identifier is required if you need to send a fax. It is not necessary if you just need to parse a disposition.
This is put in the $_POST
['id'] variable of the request to the eFax Developer website.
eFaxException | is raised if $account_id is an array or an object. |
[in] | $account_id | The eFax account identifier |
eFax::set_disposition_language | ( | $language | ) |
This function defines the language to be used in the disposition message. By default (if undefined) the language is English.
Currently supported languages are:
You may set the language to null to get the default.
eFaxException | is thrown whenever the parameter is not one of the supported languages. |
[in] | $language | One of the 2 letter name of the supported languages. |
eFax::set_disposition_level | ( | $disposition_level | ) |
This function is used to define the type of reply this request will generate.
By default, this class will force replies for both successful and erroneous request. It is strongly encouraged to keep it that way. The eFax default is NONE (i.e. zero–no reply whatsoever.)
It is possible to request no response at all.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not one of 0, eFax::RESPOND_ERROR, eFax::RESPOND_SUCCESS or a mix of these flags. |
[in] | $disposition_level | Logical OR of 0, eFax::RESPOND_ERROR and eFax::RESPOND_SUCCESS |
References RESPOND_ERROR, and RESPOND_SUCCESS.
eFax::set_disposition_method | ( | $method | ) |
This function defines the method used to send back the reply. The reply can be sent using either a POST or an EMAIL.
This parameter is optional. The default is POST. Note that more often than not, emails are not secure.
IMPORTANT NOTE: The POST method requires you to have a secure web server (HTTPS protocol) with a valid certificate (one that eFax Developer computer can check automatically.)
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not one of null, "POST", "EMAIL" or "NONE". |
[in] | $method | One of null, "POST", "EMAIL" or "NONE". |
eFax::set_disposition_url | ( | $disposition_url | ) |
This function is used to define the URL where the results of the request will be returned.
Your eFax account lets you define a default URL. This option lets you define the URL dynamically. However, be careful since the URL is limited to only 100 characters.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not a string or null or if the string is over 100 characters. |
[in] | $disposition_url | A string with the header information or null. |
eFax::set_duplicate_id | ( | $duplicate_id | ) |
This function can be used to determine whether the same fax identifier can be used multiple times (in several different requests.)
This identifier is optional. If this option is not set (i.e. null,) then identifiers can be duplicated.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not true, false or null. |
[in] | $duplicate_id | One of true, false or null. |
eFax::set_fax_header | ( | $fax_header | ) |
This function can be used to define the fax header.
eFax will provide a default if not specified.
The fax header is a string that can include @<name> and %<name> dynamic parameters. The @<name> parameters are usually specific to one fax. When the %<name> parameters are specific to each page of a fax.
The available dynamic fax header variables are:
@DATEx where x is a digit defining the format of the eFax server date (i.e. Pacific Time). y
represents the year, m
the month in digits, x
the month in letters, d
the day. All formats imply a leading 0 to years, months and days.
@TIMEx where x is a digit defining the format of the eFax server time (i.e. Pacific Time.) h represents the hour, m the minute, s the second, xx the AM/PM letters.
@ROUTETO{n} is replaced by the name of the recipient name and company. The value 'n' represents the maximum number of characters that will be printed in the header (i.e. "\@DATE3 \@TIME2 \@ROUTETO{20}" would print the date, time and up to 20 letters of the recipient name and recipient company name.) That name is looked up in the XML data:
@RCVRFAX is replaced by the fax number of the recipient. This is taken from the XML data:
@SPAGES is the total number of pages for this fax including the cover sheet if there is one.
%P is the current page number.
%nf is used to defines the size of the font. 'n' must be replaced with a digit (0 through 3, i.e. %2f) to indicate the size. 0 is the default and is the largest font. 3 is the smallest font. This tag can appear as many times as required in the header.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not a string or null or if the string is over 80 characters. |
[in] | $fax_header | A string with the header information or null. |
eFax::set_fax_id | ( | $fax_id | ) |
This function saves the request identifier used for this fax. For instance, if the fax represents an order, you can use the order number as this fax identifier.
Each identifier must be unique or eFax generates an error. If your system may end up sending the same fax multiple times (such as resending the fax for an order,) then you must make sure that each copy of that fax is given a unique identifier.
This identifier is optional. If not defined, then none gets sent. In that case, eFax generates an identifier for you.
The request identifier is saved in:
Note that it is possible to request the acceptance of faxes duplication by calling the set_duplicate_id(). The default was false before, but it may now be true.
eFaxException | is raised if the parameter fax_id is not null, a string or a numeric, or if the length is more than 15 characters. |
[in] | $fax_id | The identifier used to identify this fax |
eFax::set_outbound_url | ( | $url | ) |
This function sets the URL used to send the fax. The eFax class sets that variable to a default value that is the URL defined by eFax as their fax server.
eFaxException | is raised whenever the parameter is not a string of at least 12 characters. |
[in] | $url | The new URL to use to send the fax |
eFax::set_priority | ( | $priority | ) |
This function sets the priority of the transmission to one of NORMAL or HIGH.
This parameter is optional. The default is NORMAL.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not one of "NORMAL" or "HIGH". |
[in] | $priority | "NORMAL" or "HIGH" |
eFax::set_raw_account_id | ( | $account_id | ) |
This function is used to set the eFax account identifier. The identifier is your eFax Developer fax number.
The account identifier is required if you need to send a fax. It is not necessary if you just need to parse a disposition.
This function uses the $account_id
as is. Please, use the eFax::set_account_id()
function to make sure that the identifier does not include invalid characters.
eFax::set_account_id()
function can be used.eFaxException | is raised if $account_id is an array or an object. |
[in] | $account_id | The eFax account identifier. |
eFax::set_resolution | ( | $resolution | ) |
This function is used to set the transmission resolution. The resolution can be set to "STANDARD" or "FINE". Note that in general, eFax charges more for the "FINE" option.
The resolution is required. It is set to "STANDARD" by default so you do not need to set it if you want to send a standard fax.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not one of "STANDARD" or "FINE". |
[in] | $resolution | "STANDARD" or "FINE" |
eFax::set_self_busy | ( | $self_busy | ) |
This function defines whether the phone number can accept multiple connections at once or not. If you do not know, it is recommended that you set this value to true (which is the default).
SelfBusy is optional. If this option is not set, then faxes will be queued instead of sent when eFax is already sending a fax at the same number.
The request identifier is saved in:
eFaxException | is thrown whenever the parameter is not true, false or null. |
[in] | $self_busy | One of true, false or null. |
eFax::set_user_name | ( | $user_name | ) |
This function saves your user name (name used to login.)
The user name is required. Without a user name you cannot login and the request fails.
For outbound messages, the user name is saved in:
For inbound messages, the user name is expected in:
eFaxException | is raised if the $user_name is not a valid string. This means it is not null, not an array, not an object and has a length of at most 20 characters. |
[in] | $user_name | The login name used to connect to eFax. |
eFax::set_user_password | ( | $user_password | ) |
This function saves your user password.
The password is required. Without a password you cannot login and the request fails.
For outbound messages, the password is saved in:
For inbound messages, the password is expected in:
eFaxException | is raised if the $user_password is not a valid string. This means it is not null, not an array, not an object and has a length of at most 20 characters. |
[in] | $user_password | The password used to connect to eFax |