Nusoap makes designing web services in PHP easy. My requirement was to use Nusoap to design a web service that an external system can use to post entries in Formidable.
Nusoap can be downloaded here. Once downloaded, explode the zip file and upload *lib* folder to the project directory. We will be creating two files: postdata.php and createfpentry.php. The first file will act as the client to post requests. The latter will act as the web service server to handle post requests and insert formidable entries.
postdata.php
<?php
require_once ('lib/nusoap.php'); //imports the nusoap library
//set the post parameter
$param = array( 'fpformfieldslist' => array( 'v1' => trim($_GET['<var1>']),
'v2' => trim($_GET['<var2>']),
'v3' => trim($_GET['<var3>'])
)
);
$client = new nusoap_client('<hostname>/createfpentry.php'); //new object referring web service server
$response = $client->call('insert_fp_entry',$param); //invoke insert_fp_entry method from the server
//Process result
if($client->fault) {
//echo fault code and fault string: $client->faultcode and $client->faultstring
}
else {
echo $response; //echo response on successful web service call
}
?>
createfpentry.php
<?php
require_once ('lib/nusoap.php'); //import nusoap library
require_once( $_SERVER['DOCUMENT_ROOT'] . '/path/to/wp-load.php' ); //load WordPress from outside
$server = new soap_server; //server object
$server->register('insert_fp_entry'); //register insert_fp_entry method
function insert_fp_entry($fpformfieldslist) {
global $frm_entry, $frm_entry_meta;
$isCreated = $frm_entry->create(array(
'form_id' => 25, //replace 25 with your form id
'item_meta' => array(
152 => $fpformfieldslist['v1'], //replace 152 with your field id. You can set other fields with values equal to the different GET parameters
),
));
$result = 'Entry successfully created'; //you can make conditional edits to the response
return $result;
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
The URL to access the web service is http://<host>/postdata.php?v1=test1&v2=test2&v3=test3. This will create an entry in formidable backend. The parameter values should be escaped for special characters.
Hope this helped! I shall be posting more on this subject in future posts. Please share your experiences and let me know if there is a better way to achieve similar results.