I coded a WordPress page on which users can submit their name. After pressing submit, it will send an API request to an external API, which returns information about their name. This is a simple overview:

<form action="" method="post">
  Name: <br><input name="example" type="text" /><br>
  <input name="submit" type="submit" />
</form>

<?php
  if (isset($_POST['submit'])) {
  $example = $_REQUEST['example'];
  $result = $customcontact->lookupByName($example); /** API PHP Library */
  print_r($result);
  }
?>

For the next step, I would like to save these request and make an short overview of the history of requests made for each user. So that they can view and check previous requests without making new calls.

Can someone point me in the right direction or perhaps knows a tutorial/guide for something similar?

Thanks a lot!

Mark

  • the request answers a few fields or a big quantity of datas ? – mmm Oct 18 '17 at 13:42
  • Hey! The API can respond with JSON, HTML, or XML. Usually a JSON response contains 10 nested JSON objects with 4 nested keys each. – Mark Oct 18 '17 at 15:40

Mark,

I've had to do something similar for multiple jobs. This is a larger topic but I'll try break it down in steps and hopefully gets you going in the right direction.

I've broken the code into steps and outlined them here:

if (isset($_POST['submit'])) {
    $example = $_REQUEST['example'];
    $result  = $customcontact->lookupByName( $example );
    /** API PHP Library */

    $save_data                      = array(); //step 1
    $key                            = time(); //step 2
    $save_data[ $key ]['epoch']     = $key; //step 3
    $save_data[ $key ]['timestamp'] = date( 'Y-m-d H:i:s' ); //step 4
    $save_data[ $key ]['data']      = $result; //step 5

    $existing_log = maybe_unserialize( get_option( 'my_site_option_saved_data' ) ); //step 6

    $existing_log[] = $save_data; //step 7

    update_option( 'my_site_option_saved_data', maybe_serialize( $existing_log ) ); //step 8
}
  1. Once we have the data, create a new array.
  2. Grab a unique value (in this case, epoch time). This becomes the array key for this entry into your "log".
  3. Add the epoch time to the array as a record of when the API call was made (For more information on epoch time: https://en.wikipedia.org/wiki/Unix_time)
  4. Add a human-readable date and time to the array. I'm using a format that is friendly to SQL queries in case you need to do something to sort or query data.
  5. Add your API results to the array.
  6. We now have all the data inside the array. We go out to the database and grab the existing logged data and attempt to unserialize it (translate from database entry to array).
  7. Once we have an array of existing data, we can add a new array into it, which is our new data.
  8. Finally, we re-serialize the data and save it to the database.

A few notes:

  1. This isn't super efficient. There would be a better way, but if you need this for testing it will work.
  2. When getting the data out, look at step 6. I'm getting the data using get_option and unserializing it (converting it to an array) via maybe_unserialize.

This should get you started. Let me know if you have any questions.

Tom

EDIT

Here are the links to the WordPress functions I'm using in my answer:

Here are a few additional links to more general logging discussion and software you can use on the server-side.

  • Hey Tom, thanks for the quick reply. I think I understand the logic and the steps. I would love to learn more about this topic, do you perhaps know some specific articles/guides that go deeper on this? – Mark Oct 18 '17 at 15:48
  • @Mark I don't have specific articles that covers this topic exclusively but I'll update my answer with additional links to documentation for the functions I'm using in my answer as well as some links to logging software/discussion. – Tom Oct 18 '17 at 16:22

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.