The code is on GitHub
Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests!
Overview
Click2Fax is an App written in PHP that allows anyone to send faxes using the UnifiedAPI. It uses the Fax resource exposed by Unified API and the following request:
- Create Faxes - Send fax to any number without need of approval
Installation
In order to be able to use the Click2Fax App, your system must be compliant with the requirement below:
- Your system needs to have PHP 5.3 installed or higher.
- Your VoipNow server must be at least version 3.0
- The extension should be able to send faxes.
Setup
STEP 1: Switch to the directory that can be accessed over web (e.g /var/www/html) and download the files.
STEP 2: Proceed to allowing permissions and ownership rights.
You also need to give proper permissions and ownership to the files of the plugin - the plugin needs to be owned by the web server user and group. For example, if we use httpd as a webserver:
chmod -R 755 <PATH_TO_Click2Fax>
chown -R httpd:httpd <PATH_TO_Click2Fax>
STEP 3: Configure the App.
In order to use the App, you must configure it. To do so, you must open the file config.php
in the <PATH_TO_Click2FAX>/config/config.php
and change the following settings:
Name | Description |
---|---|
VN_SERVER_IP | The IP or hostname of your VoipNow server; |
VN_EXTENSION | The extended number of a phone terminal extension. |
OAUTH_APP_ID | OAuth App ID or key. A 32 char-long string used by the App to identify itself with the system. Visit this page to see how the OAuth APP ID can be obtained. |
OAUTH_APP_SECRET | OAuth App Secret. A 32 char-long secret used by the system to establish ownership of the App ID or key. Based on this pair, the system will recognize the new App and will allow it to generate an access token. Visit this page to see how the Secret can be obtained. |
STEP 4: Setup the system.
You also need to make sure the VoipNow system is setup to receive faxes.
Behaviour
User Interface
As soon as the setup process is complete, you can access the interface at https://<IP>/<PATH_TO_CLICK2FAX>/.
The page displays a single button that the user can click in order to send the faxes. Once the button is clicked the following form appears:
To send the fax a user must enter the information in the form fields as described below:
Field name | Required | Description |
---|---|---|
To | Yes | A list of phone numbers separated by a comma. You can set here an extension from VoipNow that accepts faxes |
Attachments | Yes | Choose a file to send as the body of the fax. |
After the form is correctly filled, the user must click the Send fax button to send the fax.
Internal Flow
When the Send Fax button is pressed the scripts makes a Create Faxes request to the VoipNow server using UnifiedAPI. It uses the extension defined in the global variable VN_EXTENSION as the source of the fax call.
First it sets the HTTP headers and the Request body accordingly:
// Fetch token $token = getToken(); $headers = array( 'Authorization' => $token ); $request = new cURLRequest(); //Fax Create uses POST $request->setMethod(cURLRequest::METHOD_POST); $request->setHeaders($headers); $request->setBody(array('recipients' => array($_POST['to'])), cURLRequest::ENCODER_JSON);
The HTTP Request made by the App looks like this:
POST /uapi/faxes/@me/0003*210 HTTP/1.1 HOST x.x.x.x Authorization: <OAUTH_ACCESS_TOKEN> Content-Length:469 Content-Type: multipart/form-data; boundary=------------325343636 ------------325343636-------- Content-Disposition:form-data; name="files"; filename="/path/to/file/file.ext" Content-Type;application/octet-stream This is my fax ------------325343636 Content-Disposition:form-data; name="request"; { "recipients":[<FAX_EXTENSION_TO>] } ------------325343636
Then sets the files which have been uploaded through the browser:
$numberOfFiles = count($_FILES['attachments']['name']); $files = array(); for($i = 0; $i < $numberOfFiles; $i++) { if($_FILES['attachments']['error'][$i] > 0) { continue; } //Renaming the file including the extension $fileext = pathinfo($_FILES['attachments']['name'][$i], PATHINFO_EXTENSION); move_uploaded_file($_FILES['attachments']['tmp_name'][$i], $_FILES['attachments']['tmp_name'][$i].'.'.$fileext); $files[] = array($_FILES['attachments']['name'][$i] => '@'.$_FILES['attachments']['tmp_name'][$i].'.'.$fileext); } $request->setFiles($files);
Any errors will be displayed on the browser.
Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.