• Resolved radu777

    (@radu777)


    So I would love to integrate a costum API from the romanian government on my website. The api will have a GET action, and in exchange for user imput of CUI (company registration code) I want a form to autofill the data it recieves from the API.

    Data includes but is not limited to: company name, address, date in which it started, current status, social capital etc.

    I have the API key and I know the codesnipped which needs to be inserted in order for the JSON data to be retrieved. I am not an expert in coding I am doing my best to understand it. At the moment I am trying to find a plugin (a form) that will autofil the information that my codesnippet is getting. What is dont know is how to make the form register the info that I am getting from the interogation, and I also dont know how to make the user input activate the function in which I add the code snippet to GET for the api.

    Sorry for the long post, if anyone can help me with any insight or training videos. I am rather limited on budget and I love to learn how to do it myself. All the video tutorials that I watched didnnt fully help me but rather provided info that i did my best to piece together to come to the conclusion above.

    Regards

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It’ll be difficult to find an existing plugin that will take data from a custom API and use it to populate a form. The required code varies by API and the specific structure of the returned data. You can use a form builder plugin to at least build the form without values and to process the submittal. You’d have to the write some JavaScript to fetch the API data and parse it into form fields.

    If your API key needs to remain secret, the request from your JavaScript would need to be relayed through the server via an Ajax request. Make requests with a XMLHttpRequest object. Such requests are easier if you use jQuery. Parse the JSON response with JSON.parse() which returns the data structure described by the JSON string. You can then set form field values based on elements of this structure.

    JavaScript code executes when certain events fire. To define what events do what, add event listeners to certain elements the user interacts with. The callback related to the listener is the code that runs when that event occurs. Again, jQuery makes it easier to add multiple event listeners based only on an element’s CSS selectors.

    An entirely different approach is to populate the fields server side in PHP as the form is being output. You can use a WP_Http class object to make API requests. PHP can parse JSON with json_decode(). Exactly how you’d do this with a form plugin varies by plugin.

    Thread Starter radu777

    (@radu777)

    I think it is back to the studying bench for me. I have quite a few concepts that i need to grasp before i can fully understand your reply. I hnderstand now that i am a long way away from implementing this.

    Thank you for your answer mate

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Sorry to overwhelm you with concepts, a solution may not be as far off as it seems. Using a plugin to help you build forms saves a lot of time. Using jQuery to make external requests, parse JSON response, and populate form fields would be the simplest way forward. There’s still a lot of niggling little details to deal with. I’m not going to say it’ll be easy, but in the end I think you’ll find it’s not as complicated as it seems right now. Hang in there!

    Thread Starter radu777

    (@radu777)

    Thank you man!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Costum API Integration’ is closed to new replies.