Seeking API Response How-To

By: Michael Clark | Asked: 06/26/2022
ForumsCategory: How-toSeeking API Response How-To
Michael ClarkMichael Clark asked 2 years ago
Hi. I'm wondering if anyone here would care to share a practical how-to on handling an API response.  Specifically, this line in the KB is what i'm looking for help on:
$returned_id = $response['body']['CHANGEME']; // this line will change based on the API you are sending to
In 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!
Question Tags:
2 Answers
Best Answer
Dan MabyDan Maby answered 2 years ago
I came up against a similar issue, so sharing the solution that worked for me in case anyone else ends up here.  In the docs, it provides the following example: 
$returned_id = $response["body"]["CHANGEME"]; // this line will change based on the API you are sending to
In 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,
	]);
  }
}
elbe phant replied 1 year ago

Thanks, this works perfect for my. I changed ["post_id"] to ["id"] to store the returned entry id from api post call.

Michael ClarkMichael Clark replied 10 months ago

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!

Victor Font answered 2 years ago
The answer depends on how you are calling the API. If you are calling the API through AJAX, you use "var results = JSON.parse(response);" to convert the returned JSON into an associative array. In PHP, you use json_decode to convert the response to an associative array.
Michael ClarkMichael Clark replied 2 years ago

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,
) );
}
}

Victor Font replied 2 years ago

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?

Michael ClarkMichael Clark replied 2 years ago

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?

Victor Font replied 2 years ago

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

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