REST API Calls (GET/POST)
Arclab® Web Form Builder
REST API
With the help of a REST API call, you can pass the form data provided by
the user to another script, web service or CRM system.
The most common
HTTP methods are GET and POST:
GET:
The data (name/value pairs) is contained in the URL:
https://www.address-of-web-service.tld/sample?name1=value1&name2=value2
POST:
The data sent to the server with POST is stored in the request body of the
HTTP request.
The data is not included in the URL.
https://www.address-of-web-service.tld/sample
Arclab® Web Form Builder also allows you to
execute custom PHP code on
form submission.
Please note that this functionality
is only available in the "Test Version" or in the "Developer Edition".
See also: Version Comparison
REST API Call (Custom PHP Code)
Switch to the "Email and Database" tab and click on
"REST API Call: GET Request" or "REST API Call:
POST Request" to add a new task:
(or select the task and click
on "Edit" to edit an existing task)
Provision or Transfer of the Form Data to the REST API
This section is identical for GET and POST requests.
First, we fill an array with data from the form. The names of the variables or fields that are passed to the REST API are on the left, while the contents (on the right) should be filled with the form inputs.
$data = array(
"Field1" => $IDxx,
"Field2" => $IDxx,
...
"Last_Field" =>
$IDxx
);
Replace $IDxx with the ID of the form field. The $IDs are displayed in the program on the right:
Sample:
$data = array(
"Name" => $ID24,
"Email" =>
$ID29,
"Company" => $ID39
);
Data Types:
Important: $IDxx contains the value as string!
If
you need the values as int or float, you have to convert them accordingly.
The following "Helper Functions" are available in the program for automatic conversion:
- for int (e.g "123"): "Sample" => ToInt($IDxx),
- for float (e.g. "1.23" or "1,23"): "Sample" => ToFloat($IDxx),
Sample: int
$data = array(
...
"Count" => (int) $IDxx,
...
);
// or:
// $data = array(
// ...
// "Count" =>
ToInt($IDxx),
// ...
// );
Sample: float
$data = array(
...
"Price" => (float) $IDxx,
...
);
// or:
// $data = array(
// ...
// "Price" => ToFloat($IDxx),
// ...
// );
If you use a comma as a decimal separator for the form field, you must replace it with a period before converting!
$data = array(
...
"Price" => (float) str_replace(",",".",$IDxx),
...
);
// or:
// $data = array(
// ...
// "Price" => ToFloat($IDxx),
// ...
// );
REST API Sample (GET)
Please note that the following is example code. The field names or variables ($IDxx) with the field contents must be adapted to your form and also to the REST API interface used. You can usually find further information or example code on the manufacturer's website.
Please note that this information is intended for developers and we cannot accept liability for incorrect code or its effects.
In the following example, PHP "curl" is used for the
API call. This is example code that you cannot transfer 1:1.
The code
only serves to show the basic procedure. Please note the code provided by
the respective manufacturer.
// Important: $IDxx contains
the value as string!
// If you need the values as int or float, you have
to convert them accordingly.
//
// for int (e.g "123"): "Sample" =>
(int) $IDxx,
// for float (e.g. "1.23"): "Sample" => (float) $IDxx,
// for float with comma as decimal separator (e.g. "1,23"): "Sample" =>
(float) str_replace(",",".",$IDxx),
//
// The following "Helper Functions"
are available in the program for automatic conversion:
// for int (e.g
"123"): "Sample" => ToInt($IDxx),
// for float (e.g. "1.23" or "1,23"):
"Sample" => ToFloat($IDxx),
// form data
$data = array(
"Name" => $ID24,
"Email" => $ID29,
"Company" => $ID39
);
// Set the URL for the REST API call without
parameters.
// The parameters are generated from the $data array and
automatically added to the URL.
$url = "https://www.address-of-web-service.tld/sample";
$url = sprintf("%s?%s", $url, http_build_query($data));
$curl = curl_init();
if ($curl === false) { ErrorCancel("PHP curl_init failed"); }
// optional authentication:
// curl_setopt($curl,
CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($curl, CURLOPT_USERPWD,
"username:password");
// optional
disable host/SSL verification
// if the hostname does not match the hostname
in the certificate
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl,
CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
// optional for JSON response
// curl_setopt($curl,
CURLOPT_HTTPHEADER, array("Accept: application/json"));
// in some cases you also need to send an API
key or other values in the HTTP header:
// curl_setopt($curl, CURLOPT_HTTPHEADER,
array(..., "X-API-Key: ..."));
$result = curl_exec($curl);
$curl_close($curl);
unset($curl);
if ($result === false) { ErrorCancel("PHP
curl failed"); }
// for JSON response
// $decoded=json_decode($result);
// further error handling
// if ( /* error
condition */ ) ErrorCancel("Error Message");
Important: Don't call "return" ... use "ErrorCancel($error);" for error processing!
REST API Sample (POST)
Please note that the following is example code. The field names or variables ($IDxx) with the field contents must be adapted to your form and also to the REST API interface used. You can usually find further information or example code on the manufacturer's website.
Please note that this information is intended for developers and we cannot accept liability for incorrect code or its effects.
In the following example, PHP "curl" is used for the
API call. This is example code that you cannot transfer 1:1.
The code
only serves to show the basic procedure. Please note the code provided by
the respective manufacturer.
// Important: $IDxx contains
the values as string!
// If you need the values as int or float, you
have to convert them accordingly.
//
// for int (e.g "123"): "Sample"
=> (int) $IDxx,
// for float (e.g. "1.23"): "Sample" => (float)
$IDxx,
// for float with comma as decimal separator (e.g. "1,23"): "Sample"
=> (float) str_replace(",",".",$IDxx),
//
// The following "Helper
Functions" are available in the program for automatic conversion:
//
for int (e.g "123"): "Sample" => ToInt($IDxx),
// for float (e.g.
"1.23" or "1,23"): "Sample" => ToFloat($IDxx),
// form data
$data = array(
"Name" => $ID24,
"Email" => $ID29,
"Company" => $ID39
);
// Set the URL for the REST
API call.
$url = "https://www.address-of-web-service.tld/sample";
$curl = curl_init();
if ($curl === false) { ErrorCancel("PHP curl_init
failed"); }
// optional authentication:
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($curl,
CURLOPT_USERPWD, "username:password");
// optional disable host/SSL verification
// if the hostname does not match the hostname in the certificate
//
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($curl,
CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL,
$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,
CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
// for JSON data:
// curl_setopt($curl, CURLOPT_POSTFIELDS,
json_encode($data));
// optional
for JSON data and response:
// curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
// in some cases you also need to send an
API key or other values in the HTTP header:
// curl_setopt($curl, CURLOPT_HTTPHEADER,
array(..., "X-API-Key: ..."));
$result = curl_exec($curl);
$curl_close($curl);
unset($curl);
if ($result === false) { ErrorCancel("PHP
curl failed"); }
// for JSON response
// $decoded=json_decode($result);
// further error handling
// if ( /* error
condition */ ) ErrorCancel("Error Message");
Important: Don't call "return" ... use "ErrorCancel($error);" for error processing!
Error Handling
After the tasks have been executed the PHP script returns either "OK" or an error code (text) to the Javascript form, which then displays either the success or the error page. If you want to stop the processing use "ErrorCancel($error)", but don't call "return" directly.
Do not use "return" under any circumstances, neither in case of error nor success!