How to Send a Fax With PHP
- 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"; - 1). 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; - 2). Invoke the SOAP method to send the fax and capture the result. For example:
$fax_result = $fax->SendCharFax($params); - 3). 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;
?> - 1). 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) . "&"; - 2). 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); - 3). 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"]);
?>
Set Up Fax Service
Send Fax With SOAP
Send Fax With REST
Source...