Two endpoints

By: Ronnie H | Asked: 12/21/2022
ForumsCategory: Code HelpTwo endpoints
Ronnie H asked 1 year ago

Hello I have a wp website and I have created a referral portal for a partner using formidable forms. Now they would like for us to push data directly into their system using endpoints. Im trying to figure out how to accomplish this. Here are the instructions they sent to me. Below is the request from the other dev team: 

 

Thanks for the meeting earlier. To summarize, I believe we will want two endpoints, one we can use to send in new referrals and one which will provide a list of existing referrals. We are using the existing portal to inform these endpoints. Here is how I see them, please let me know if you see any issues or have questions:   POST SendReferral input: { "firstName": "John", "lastName": "Doe", "referralMadeDate": "2022-12-06T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone "phoneNumber": "6185551234", // no symbols, 10 digits "emailAddress": "[email protected]", "comments": "" // keeping for consistency with the UI }   output: { "status": "success", // I don't see a need for statuses beyond "success" or "error" at this point "message": "", // I don't see a need for a message on success; on error this could indicate any error condition that prevented the record from being created (duplicate, for example) "id": 1 // any ID format will be fine, let us know if it is integer, GUID, or something else }   GET GetReferrals input: // none needed, below a potential improvement: { "referralMadeStartDate": "2022-11-01T00:00:00.000Z", "referralMadeEndDate": "2022-11-01T23:59:59.999Z" } output: [ { "id": 1, // any ID format will be fine, let us know if it is integer, GUID, or something else "firstName": "John", "lastName": "Doe", "referralMadeDate": "2022-11-06T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone "assignedDate": "2022-11-07T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone "paidDate": "2022-11-09T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone "paidAmount": 750.00 }, { "id": 2, // any ID format will be fine, let us know if it is integer, GUID, or something else "firstName": "Jane", "lastName": "Doe", "referralMadeDate": "2022-12-01T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone "assignedDate": null, // null if unassigned "paidDate": null, // example of unpaid "paidAmount": null // null if unpaid } ]   A couple things I will call out specifically: We will want to have an ID to tie this to our system internally. If you can return a unique ID on success of a referral which matches what we would later get back when calling the endpoint to list referrals, that would do perfectly. It can be any format or data type as long as it is unique and will remain the same for the life of the referral.
When calling the endpoint to retrieve referrals, on our side we would be fine with sending no request body and getting back the list of all referrals. If this proves too cumbersome on network traffic or data lookup, sending in parameters for start date and end date to retrieve referrals with referralMadeDates within that range would be a good next step. If this seems reasonable to implement to start, I would be happy starting off with implementing date parameters out of the gate. Please let me know if you have any questions or concerns.    

2 Answers
Victor Font answered 1 year ago
Endpoints are remote data locations available through a REST API. REST API is an acronym for Representational State Transfer Application Programming Interface. All this means is means when a RESTful API is called, the server will transfer a representation of the requested resource's state to the client system. Accessing API endpoints generally requires access credentials and the request/response content formatted specifically to the target system design requirements. There are many ways to initiate an exchange with an endpoint. Ideally, the responses should be received as JSON. If your design includes exchanging data in real time when form entries are created or updated, the you need the Formidable API add-on. Depending on the complexity of the data flow or if your pulling lists that may require a lot of processing, you may be required to create custom code to take advantage of the WordPress WP_Remote Class. It may be worth the time to pay to have your requirements and design reviewed by an experienced Formidable API developer before you decide on the right path. You can find them in the Formidable Masterminds Developers Directory
Victor Font replied 1 year ago

Here's the same formatted before the formatting was stripped out:

Endpoints are remote data locations available through a REST API. REST API is an acronym for <strong>Representational State Transfer Application Programming Interface</strong>. All this means is means when a RESTful API is called, the server will transfer a representation of the requested resource's state to the client system.

Accessing API endpoints generally requires access credentials and the request/response content formatted specifically to the target system design requirements. There are many ways to initiate an exchange with an endpoint. Ideally, the responses should be received as JSON.

If your design includes exchanging data in real time when form entries are created or updated, then you need the Formidable API add-on. Depending on the complexity of the data flow or if your pulling lists that may require a lot of processing, you may be required to create custom code to take advantage of the WordPress WP_Remote Class.

It may be worth the time to pay to have your requirements and design reviewed by an experienced Formidable API developer before you decide on the right path. You can find them in the Formidable Masterminds Developers Directory at https://formidable-masterminds.com/developers-directory/

Ronnie H replied 1 year ago

Awesome thank you I have already installed the formidable API but the endpoint was not in the correct format

{"id":"44","item_key":"efxqr","name":"Ibrahin","ip":"209.64.41.13","meta":{"qyh7k":"Ibrahin","5xxqb":"Sidibe","2hnzz":"2022-12-19","gke7d":"(301)-530-1600","9qpmd":"[email protected]","dxjaa":"","sw20r":"","ksnik":"","ypk17":"Had a Spinal Chord injury","uq5ko":"MARYLAND\r\n12\/20\/22 Made contact for bene's interest in program, phone number is for a business, and phone kept ringing, could not leave a voicemail.","re8y3":"allsup","re8y3-value":"2"},"form_id":"2","post_id":"0","user_id":"2","parent_item_id":"0","is_draft":"0","updated_by":"3","created_at":"2022-12-19 20:18:55","updated_at":"2022-12-20 15:35:18"}

So I will contact the developers to help assist me further, thanks

Ronnie H replied 1 year ago

How much would something like this cost based on the project description I have added?

Ronnie H replied 1 year ago

Ok so I have been working on this and I have noticed that in the sql database the form entries are stored in two different locations wp_frm_item and wp_frm_items_meta so when writing my endpoint this created some difficulty but here is the code I have thus far. Any recommendations on getting this into a json array?
Below are the formidable entry fields we need in the endpoint. I have attached the code in the anser after this post thanks.
"firstName": "John",

"lastName": "Doe",

"referralMadeDate": "2022-11-06T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone

"assignedDate": "2022-11-07T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone

"paidDate": "2022-11-09T20:43:04.418Z", // ISO format, Z timezone indicating GMT timezone

"paidAmount": 750.00

Ronnie H answered 1 year ago
Start your code here 

function getFormData() {
    global $wpdb;
$result = $wpdb->get_results('select meta_value, field_id, item_id from wp_frm_items fi inner join wp_frm_items_meta fim ' .
'on (fi.id = fim.item_id)');

$output = array();
$item = new stdClass();
foreach($result as $row) {
$item->id = $row->id;
switch ($row->field_id) {
case 6: $item->first_name = $row->meta_value; break;
case 7: $item->first_name = $row->meta_value; break;
case 14: $item->date = $row->meta_value; break;
case 8: $item->phone_number = $row->meta_value; break;
case 10: $item->email_address = $row->meta_value; break;
case 9: $item->description = $row->meta_value; break;
default: array_push($item); $item = new stdClass(); break;
}
}

return json_encode($output, JSON_PRETTY_PRINT);
}
Victor Font replied 1 year ago

I don't even know how this query is returning any values, you're not using aliases in the SELECT. Also, why did you use an INNER JOIN instead of LEFT JOIN?

Ronnie H replied 1 year ago

return $output;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/formitem/list', array(
'methods' => 'GET',
'callback' => 'getFormData',
) );
});

Ronnie H replied 1 year ago

Sorry I left out some of the code above are the return values thanks

Ronnie H replied 1 year ago

New question I have figured that part out. Thanks for your help

Does formidable have the capability for POST Send endpoints? How should the form data be manipulated to add form data from an endpoint?

thanks again

Victor Font replied 1 year ago

Formidable does not have its own REST API. It provides its own endpoints for the WordPress API. Anything that can be done with the WordPress API can be done for Formidable's data. So yes, you can use the HEAD, GET, POST, PUT, and DELETE methods for your request. Here's a link to the https://developer.wordpress.org/rest-api/

Ronnie H replied 1 year ago

Hey thanks Victor

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right