Linx Main Linx Help

Call Linx from a PHP page

Had a discussion yesterday with a friend and we were talking about calling a Linx exposed REST service from a PHP page. Here is an example of how I did it a while ago. This was the quickest way to do it, however, there might be other ways as well:

In this case, it was a simple demonstration where I captured a persons details and sent it to a Linx process to manage the data. No encryption was done, because it was a demo.

In the PHP page, just after the tag and before any other contents I used:

<?php
// define variables and set to empty values
$nameErr = $surnameErr = $emailErr = $phoneErr = $passwordErr = $confirmpasswordErr = "";
$name = $surname = $email = $phone = $password = $confirmpassword = "";
$errormessage = "";

$CanContinue = TRUE;

// Here we check if the Submit button was pressed on the form
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
  //First we check if the required fields were entered correctly
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
    $CanContinue = FALSE;
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["surname"])) {
    $surnameErr = "Surname is required";
    $CanContinue = FALSE;
  } else {
    $surname = test_input($_POST["surname"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    $CanContinue = FALSE;
  } else {
     $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }
  if (empty($_POST["phone"])) {
    $phoneErr = "";
  } else {
    $phone = test_input($_POST["phone"]);
  }
if (empty($_POST["password"])) {
    $passwordErr = "Password is required";
    $CanContinue = FALSE;
  } else {
    $password = test_input($_POST["password"]);
  }
  if (empty($_POST["confirmpassword"])) {
    $confirmpasswordErr = "Please confirm password";
    $CanContinue = FALSE;
  } else if ($_POST["confirmpassword"] != $_POST["password"])
   {
        $confirmpasswordErr = "Passwords do not match";
        $CanContinue = FALSE;
   }
   else
   {
    $confirmpassword = test_input($_POST["confirmpassword"]);
   }

//Now we can finally do the call to LINX
if ($CanContinue === TRUE)
{
   //First the URL as you've set it up on your Linx server. This can be anywhere, as long as the PHP server can
   // reach it.
   $url = "http://linxserver.com/MyService/UserInfo/RegOnline";    
  // Now the content of the REST service request is created as a JSON string
   $content = '{"sName":"'.$name.'","sSurname": "'.$surname.'","sEmail": "'.$email.'","sUsername": "'.$email.'", "sPhose": "'.$phone.'","sPass": "'.$password.'","iUserType": 1,"iFP": 0,"sFP": "","sG": "", "sCode": "RegOnline"}';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
    array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);


curl_close($curl);

//We get the response back from Linx (In JSON in this case)
$response = json_decode($json_response, true);

$success = $response['Successful'];

if ($success == True)
{
echo "<script>window.location = 'thank-you-sign-up.php'</script>";
}
else
  {
    $errormessage = $response['ErrorMessage'];
  }
 }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

Somewhere in our page we can then also show the error message if Linx sent back an error:

<?php
 echo "<h2></h2>";
 echo $errormessage;
?>