Mon. - Fri. 9 am - 5 pm Ask a Question
Remember me

POST Submitted Data to an API or URL

1 week 22 hours ago #1

  • tony's Avatar
  • tony
  • Posts: 45
HI, is it possible to send form submissions to an API endpoint? I found an example script from elsewhere shared below. Would this work in the PHP Scripts -> After Form Submission area of a form.

// Populate the data that will be sent
$data = [
'first_name' => $submission->params,
'last_name' => $submission->params,
'email' => $submission->params
];

// Set the URL that will retrieve the data
$url = 'api.example.com/accounts';

$response = JHttpFactory::getHttp()->post($url, $data);

// Do something if the data were sent successfully
if ($response->code == 200)
{
// Your code..
}

1 week 22 hours ago #2

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 18983
Hello,
Thank you for contacting us

In your code, you need to replace
$submission->params
With quotes, which will contain the data tag (the field you need)
Example:
"[Field ID=1]"

Regards,
Vyacheslav, Balbooa.com

1 week 13 hours ago #3

  • tony's Avatar
  • tony
  • Posts: 45
Ok great, so is this below example how you mean?

$data = [
'first_name' => "[Field ID=22]",
'last_name' => "[Field ID=35]",
'phone' => "[Field ID=24]",
'email' => "[Field ID=25]"
];

Are the below parts required as these are from an example?

'first_name' =>
'last_name' =>
'phone' =>
'email' =>


Thank you

6 days 22 hours ago #4

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 18983
That's right, you need to use:
$data = [
'first_name' => "[Field ID=22]",
'last_name' => "[Field ID=35]",
'phone' => "[Field ID=24]",
'email' => "[Field ID=25]"
];

Regards,
Vyacheslav, Balbooa.com

6 days 15 hours ago #5

  • tony's Avatar
  • tony
  • Posts: 45
ok and these below are for the recipients api field names?

'first_name' =>
'last_name' =>
'phone' =>
'email' =>

6 days 15 hours ago #6

  • tony's Avatar
  • tony
  • Posts: 45
Hi again, sorry im not 100% with these things but how do we add a HASHKEY key with a value “eg.value-will-be-this” just for authentication?

I was told to include it in the body JSON as a key value pair

5 days 23 hours ago #7

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 18983
You can add them separated by commas
$data = [
'first_name' => "[Field ID=22]",
'last_name' => "[Field ID=35]",
'phone' => "[Field ID=24]",
'email' => "[Field ID=25]",
....... ,
....... ,
];

Best Regards,
Vyacheslav, Balbooa Support Team

5 days 16 hours ago #8

  • tony's Avatar
  • tony
  • Posts: 45
That's great :)

All worked perfectly, data sent and retrieved at the other end.

Thank you for the great support

5 days 16 hours ago #9

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 18983
You are welcome ;)

If you like our Form Builder and support, please take a moment to rate it! :cheer:
extensions.joomla.org/extension/forms/

Let us know if you need more assistance!

Best Regards,
Vyacheslav, Balbooa Support Team
Powered by Kunena Forum