PHP code example of valconsultby / byfax-php

1. Go to this page and download the library: Download valconsultby/byfax-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

valconsultby / byfax-php example snippets


php composer.phar 

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "document " API service and pass API key-secret and url data
$serviceDoc = new \byfax\ApiServiceDocumentSoapClient($apiKey, $apiSecret, $apiUrl);

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "cover" API service and pass API key-secret and url data
$serviceCover = new \byfax\ApiServiceCoverSoapClient($apiKey, $apiSecret, $apiUrl);

// call "listCovers" function and obtain response
$response = $serviceCover->listCovers();

// Check response state code for success and return false if failed
if ($response->stateCode != StateCodes::SUCCESS)
      return false;

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "cover" API service and pass API key-secret and url data
$serviceCover = new \byfax\ApiServiceCoverSoapClient($apiKey, $apiSecret, $apiUrl);

$filename = "/path/to/user/cover.docx";

// Create and fill FaxFile object
$coverDoc = new \byfax\model\entity\common\FaxFile();
$coverDoc->fileSize = filesize($filename);
$coverDoc->fileCheck = md5_file($filename);
$coverDoc->fileName = basename($filename);
$coverDoc->fileMime = mime_content_type($filename);
$coverDoc->fileData = file_get_contents($filename); // Read file data without encoding to Base64

// Call Api function "addCover" to upload a new cover page
$response = $serviceCover->addCover("Test cover name", $coverDoc);

// Check response state code for success and return false if failed
if ($response->stateCode != StateCodes::SUCCESS)
      return false;

// Store newly created cover page refID
$coverReference = response->objectRef;

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "document " API service and pass API key-secret and url data
$serviceDoc = new \byfax\ApiServiceDocumentSoapClient($apiKey, $apiSecret, $apiUrl);

$filename = "/path/to/user/file.pdf";

// Create and fill FaxDocument object
$uploadDoc = new \byfax\model\entity\common\FaxDocument();
$uploadDoc->documentFile = new \byfax\model\entity\common\FaxFile();

$uploadDoc->documentFile->fileSize = filesize($filename);
$uploadDoc->documentFile->fileCheck = md5_file($filename);
$uploadDoc->documentFile->fileName = basename($filename);
$uploadDoc->documentFile->fileMime = mime_content_type($filename);
$uploadDoc->documentFile->fileData = file_get_contents($filename); // Read file data without encoding to Base64

// Call Api function "uploadDocument" to upload a document to cache
$response = $serviceDoc->uploadDocument($uploadDoc);

// Check response state code for success and return false if failed
if ($response->stateCode != StateCodes::SUCCESS)
      return false;

// Store uploaded document refID for firther use
$uploadedRef = $response->fileRef;

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "faxout" API service and pass API key-secret and url data
$serviceFax = new \byfax\ApiServiceFaxoutSoapClient($apiKey, $apiSecret, $apiUrl);

// Crate and fill fax submission request data
$submitRequest = new \byfax\model\request\faxout\ApiRequestFaxjobSubmit();

// Submission broadcast refID.
// Unique within your API account
// Should be unique for each submission
// Uncomment this line to set at your side otherwise API will generate.
// $submitRequest->broadcastRef = md5("my-broadcast-ref" . microtime(true));

// Fax header format template
$submitRequest->Header = "<DateTime> <Timezone>|From: <From> To: <To>|Page <CurPage>of <CurPages>";

// Sender timezone
$submitRequest->Timezone = "Europe/Minsk";

// Number of reties in case of Busy or NoAnswer
$submitRequest->busyRetry = 3;

// Send quality. Available options are STANDARD or FINE. See the enumeration
$submitRequest->sendQuality = \byfax\enum\FaxQuality::FINE;

// Send mode. Available options are TEXT or PHOTO. See the enumeration
$submitRequest->sendMode = \byfax\enum\FaxMode::TEXT;

// Cover page if needed to add to fax, skip otherwise. Use listCovers to obtain a cover RefID.
$submitRequest->Cover = new \byfax\model\entity\cover\FaxCover();
$submitRequest->Cover->coverRef = "COVER_PAGE_REF_ID";

// Sender identification. At least one of properties is ubmitDoc->documentFile->fileMime = mime_content_type($filePath);
$submitDoc->documentFile->fileData = file_get_contents($filePath);

// Push document object into the array.
$submitRequest->Documents[] = $submitDoc;

// Call "Submit" API function to push fax into the queue
$response = $serviceFax->Submit($submitRequest);

// Check response state code for success and return false if failed
if ($response->stateCode != StateCodes::SUCCESS)
      return false;

// Create and fill check-state request for a single recipient
$checkRequest = new \byfax\model\request\faxout\ApiRequestFaxjobListMessages();
$checkRequest->messageRefs[] = $response->reportRecipients[0]->messageRef;

// Add and fill pagination data
$checkRequest->pagination = new \byfax\model\entity\common\ListPagination();
$checkRequest->pagination->pageNumber = 0;
$checkRequest->pagination->pageSize = 10;

// Call "listRecipients" API function to obtain recipients status
$checkResponse = $serviceFaxout->listRecipients($checkRequest);

// Check response state code for success and return false if failed
if ($checkResponse->stateCode != StateCodes::SUCCESS)
return false;

// Obtain and store recipient status
$recipientStatus = $checkResponse->Messages[0]->jobState;

$apiKey = "YOUR-API-KEY";
$apiSecret = "YOUR-API-SECRET";
$apiUrl = "https://sandbox.byfax.biz"; // Replace with https://api.byfax.biz for production

// Create "faxout" API service and pass API key-secret and url data
$serviceFax = new \byfax\ApiServiceFaxoutSoapClient($apiKey, $apiSecret, $apiUrl);

// Crate and fill fax submission request data
$submitRequest = new \byfax\model\request\faxout\ApiRequestFaxjobMessage();

// Submission broadcast refID.
// Unique within your API account
// Should be unique for each submission
// Uncomment this line to set at your side otherwise API will generate.
// $submitRequest->broadcastRef = md5("my-broadcast-ref" . microtime(true));

// Submission message refID.
// Unique within your API account
// Should be unique for each recipient withn the entire account
// Uncomment this line to set at your side otherwise API will generate.
// $submitRequest->messageRef = md5("my-message-ref" . microtime(true));

// Fax header format template
$submitRequest->Header = "<DateTime> <Timezone>|From: <From> To: <To>|Page <CurPage>of <CurPages>";

// Sender timezone
$submitRequest->Timezone = "Europe/Minsk";

// Number of reties in case of Busy or NoAnswer
$submitRequest->busyRetry = 3;

// Sender identification. At least one of properties is itMessage($submitRequest);

// Check response state code for success and return false if failed
if ($response->stateCode != StateCodes::SUCCESS)
      return false;


// Create and fill check-state request for a single recipient
$checkRequest = new \byfax\model\request\faxout\ApiRequestFaxjobListMessages();
$checkRequest->messageRefs[] = $response->reportRecipients[0]->messageRef;

// Add and fill pagination data
$checkRequest->pagination = new \byfax\model\entity\common\ListPagination();
$checkRequest->pagination->pageNumber = 0;
$checkRequest->pagination->pageSize = 10;

// Call "listRecipients" API function to obtain recipients status
$checkResponse = $serviceFaxout->listRecipients($checkRequest);

// Check response state code for success and return false if failed
if ($checkResponse->stateCode != StateCodes::SUCCESS)
return false;

// Obtain and store recipient status
$recipientStatus = $checkResponse->Messages[0]->jobState;