How to Send a Fax With PHP

How to Send a Fax With PHP thumbnail
Write a PHP script to send a fax.

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

  1. 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"]);

      ?>

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.

Related Searches:

References

Resources

  • Photo Credit fax image by Ewe Degiampietro from Fotolia.com

Comments

You May Also Like

  • How to Use Thermal Fax Paper for Tattoos

    Tattoos are a type of body art in which colored pigments are inserted under the skin to create a permanent design. The...

  • How to Send SMS Through PHP

    SMS, or short message service, is a communication service available to mobile phone subscribers. It allows users to send snippets of text...

  • Fax Protocol

    If you want to send a business document fast, send it by fax. Faxing is an effective way to transmit information from...

  • How to Interface PHP and MySQL

    PHP is a Web-development scripting language used for generating dynamic Web pages. MySQL is a database server that supports efficient multi-threading (simultaneous...

  • How to Send a Fax to a Web Service

    Online fax services make it unnecessary to have a fax machine and a fax modem today. You can send faxes online with...

  • How to Send an Internet Fax Using E-mail

    You can skip the fax machine altogether when you send an Internet fax using e-mail. As long as you have the document...

  • How to Identify PHP Protocol

    When a Web server runs a PHP script, or serves an HTML Web page with snippets of PHP code interspersed throughout the...

  • How to Send Text Message as a Fax With a Cell Phone

    Text messaging is pretty much synonymous for communication. It has gained a lot of popularity within the last few years due to...

  • How to Send an SMS with a PHP Script

    With nearly everyone carrying a cellular phone, Short Message Service (SMS) is a powerful way to reach people wherever they are. Your...

  • How to View a Word Document From PHP

    Word documents are rich-text documents that allow you to create newsletters, contracts and other customer communication. You can display these Word documents...

Related Ads

Featured