Need to pull data from external API for forms

Permalink
I am hoping someone can put me in the right direction here. I have a site I am developing in 8.5.1 that has an Enrollment form. Dropdowns in this form need to be populated with data that resides on a different server. The server runs an application called 4D. There is an API already supplying data to the clients Wordpress sites.

I am rebuilding their sites in C5 and need the same functionality. I have never done this before and am not sure where to start in C5. Really just need to do a GET. We are going to store the data locally, and run a CRON or something to POST the data back to the 4D server every 10 minutes or so. This is because the 4D server does have a tendency to go down from time to time.

Can anyone help with this?

 
mesuva replied on at Permalink Reply
mesuva
I've done something similar to this where we've had some external data in a Laravel website, and we've fetched JSON of that data with an automated job. That data was then stored in concrete5's config, where some other form fetched it for display purposes.

So I'd suggest checking out the doco on automated jobs:
https://documentation.concrete5.org/developers/jobs/creating-a-job...
Automated jobs give you a nice way to run code in a way that gives access to concrete5's API, to schedule them, do logging, etc.

Here's a very stripped back version of the automated job we created. It originally was in a package, and did a whole bunch of other stuff, but I've cut it back to act more as an example. There are going to be ways to tidy this up further (like replacing the native cURL call with concrete5's helper), but it hopefully can act as a starting point:

<?php
namespace Application\Job;
use \Concrete\Core\Job\Job as AbstractJob;
class SyncData extends AbstractJob {
    var $baseurl = 'https://yourremotesource.com';
    public function getJobName() {
        return t("Synchronise Data");
    }
    public function getJobDescription() {
        return t("Synchronises data from a remote source");
    }
    public function run() {
        return $this->updateList();
    }
    private function updateList() {


Personally I'd also put it in it's own package.
GrizzlyAdams replied on at Permalink Reply
How about just pulling data from a second database on the same server? We have a separate DB that is updated manually with the values.

I need that data to use in a custom "Quote" form.

I am starting with this...
<?php
// we connect to example.com and port 3306
$link = mysqli_connect('localhost', 'user', 'passowrd');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = "SELECT * FROM Products_Premiums";
$result = mysqli_query($query);
echo $result;
mysqli_close($link);
?>


I pulled out the creds in this example for security reasons. And I do get the "Connected Successfully" message.

However, I cannot get any data.

Any thoughts that might help me? I have searched and searched but have not found a good example or step by step.
mesuva replied on at Permalink Reply
mesuva
I'd recommend you use concrete5's approach to connecting to the DB, rather than using a raw mysql connection like that.

You would define the secondary connection using these instructions:
https://documentation.concrete5.org/developers/database-management/c...

and then refer here to do your queries (but specifying your db connection):
https://documentation.concrete5.org/developers/database-management/a...