$returned_id = $response['body']['CHANGEME']; // this line will change based on the API you are sending toIn many cases, the response body, is usually a structured JSON payload with key:value pairs. So if the API I'm pinging returns status OK (200) with
'{"fname":"Jane","lname":"Jameson"}'How do I change the line above, the "$returned_id=$response.." part so I can save "Jane" to a field? Thanks in advance!
$returned_id = $response["body"]["CHANGEME"]; // this line will change based on the API you are sending toIn my case, I wanted to get the post_id from the returned response given when successfully connecting to another Formidable Form on a separate site. I used json_decode to convert the response, this is an example of the resulting code:
add_action("frmapi_post_response", "frm_save_api_response", 10, 3); /** * Formidable Forms - API save a value from the response * * @since 1.0 * @url https://formidableforms.com/knowledgebase/frm_api_post_response/#kb-save-a-value-from-the-response **/ function frm_save_api_response($response, $entry, $form_action) { $body = json_decode($response["body"], true); // convert the response $returned_id = $body["post_id"]; // this line will change based on the API you are sending to if ($returned_id) { FrmProEntryMeta::update_single_field([ "entry_id" => $entry->id, "field_id" => 25, // change to the ID of the field to change "value" => $returned_id, ]); } }
Thanks, this works perfect for my. I changed ["post_id"] to ["id"] to store the returned entry id from api post call.
Dan! Really appreciate you sharing a simple, real use case and being very specific about the example. This gave me a far greater practical understanding *in the context of the sample code found in the FF knowledgebase*. Since I'm not a proficient coder, and am left to decipher and experiment, this your example made this more intelligible for me. Thank you!
Calling the API through Formidable's very handy API plugin, trying to understand how to tell frm_api_post_response what I want and where to store it
add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
$returned_id = $response['body']['CHANGEME']; // this line will change based on the API you are sending to
if ( $returned_id ) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 25, // change to the ID of the field to change
'value' => $returned_id,
) );
}
}
From the fact that $response['body']['CHANGEME'] is an associative array, my guess is Formidable converts the Json response before passing it into the callback. Have you examined the value of $response?
Yep. When I look at the logs, I can see the JSON response. in the body, I can see the name:value pairs. Just don't know enough to understand how to write what I value from the JSON payload I want to save in the CHANGEME part. So, if the name: in the JSON is {"fname":"Jane","lname":"Jameson"}, and I want the value of fname, what do I put in place of the CHANGEME in the example code?
The logs capture the raw response from the API call. It does not show you the value of the $response associative array after Formidable creates it an passes it as a filter parameter to your callback. You need to examine the contents of $response in PHP using a debugger that allows you to set a breakpoint and suspend code execution so you can visualize all the callback parameters.
If you're not using a real time debugger like Kint or Xdebug, use either of the PHP functions print_r() or var_dump().
https://www.php.net/manual/en/function.print-r.php
https://www.php.net/manual/en/function.var-dump
Please login or Register to submit your answer