How to Send a Fax With PHP
The hypertext preprocessor (PHP) scripting language has built-in support for email, but not faxing. To fax using PHP, you need to purchase a fax web service form a third-party provider and use its PHP functions to access the service and send the fax. The web services will typically be provided in PHP through either a simple object access protocol (SOAP) or representational state transfer (REST) interface.
Instructions
-
Set Up Fax Service
-
1
Choose an Internet fax service to use and register as a developer if necessary. Confirm that you meet the minimum requirements of the fax service, for example PHP version 5.0 or higher with SOAP or extensible markup language (XML) enabled.
-
2
Review the documentation for the web service and gather the information you need to send the fax. For example, you might need the fax number, whether the fax consists of pure text or HTML, the text to fax and either the username and password to the fax service or a send authorization code.
-
-
3
Create a PHP script with an editor and begin by assigning each piece of information to a variable, for example:
<?php
$fax_number = "800-555-1212";
$fax_text = "This is a test fax.";
$fax_type = "text";
$my_username = "myusername";
$my_password = "mypassword";
$send_code = "mysendcode";
Send Fax With SOAP
-
4
Create a new SOAP object using the web service definition language (WSDL) in the documentation provided by the fax service. Assign the appropriate variables to the object's members. For example:
$fax = new SoapClient("http://ws.interfax.net/dfs.asmx?wsdl");
$params->Username = $my_username;
$params->Password = $my_password;
$params->FaxNumber = $fax_number;
$params->Data = $fax_text;
$params->FileType = $fax_type;
-
5
Invoke the SOAP method to send the fax and capture the result. For example:
$fax_result = $fax->SendCharFax($params);
-
6
Check the result and notify the user of the status. For example:
if ($fax_result < 0)
die("Fax Unsuccessful! Error code is " . $fax_result);
else
echo "Fax Successful! Transaction ID is " . $fax_result;
?>
Send Fax With REST
-
7
Build the uniform resource locator (URL) string with the variables you created. For example:
$url = "http://www.interfax.net/sendfax?";
$params = array("Username" => $my_username, "Password" => $my_password, "FaxNumber" => $fax_number, "Data" => $fax_text, "FileType", $fax_type);
foreach ($params as $key=>$value) $url .= $key . "=" . urlencode($value) . "&";
-
8
Invoke the URL by using either "file_get_contents" or Curl. For example:
$result = file_get_contents($url);
or
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
curl_close($c);
-
9
Parse the XML result into an array and check the resulting value or status. For example:
$result_array = xml_parser_create();
xml_parse_into_struct($result_array, $result, $values, $index);
xml_parser_free($parse);
if ($values["RESULT"] > 0)
echo "Fax Successful!";
else
die("Fax Unsuccessful! Error is " . $values["ERROR"]);
?>
-
1
Tips & Warnings
If you sign up for a fax service that allows fax by email, you can alternatively fax by a document with PHP by emailing it to the fax email address with a single command. For example:
$result = mail("fax@faxmailaddress.com", "sendcode", "Here is the text to fax");
A fax can fail after you've received a successful fax return result from the interface, for example if the recipient's fax machine is out of paper and not accepting faxes. Log in to your fax service account and check the fax result to ensure it was received.
References
Resources
- Photo Credit fax image by Ewe Degiampietro from Fotolia.com