pythonbashpowershellrestshell

How to make a script to automate taking the GET data from my wordpress site and POST to another server


I have this problem and I'm a newbie of REST API world. I have a Wordpress site in which I sell CD Albums, and I have to take every orders of the day and send some specific data (like the bardcode/ean, the name of the album, the sales quantity, etc etc) to a server, sending this data with REST API calls. I have never used it before but I am trying to learn it.

So, to take the data of my Wordpress site I send a GET request with the Wordpress API and receive a JSON file with all the necessary info. But of all this information I just have to extrapolate some of it and then send them all to this server with a POST request. (The server and API are Korean and are from Hanteo Chart).

The main problem is that I have to write a script to automate the whole process and I don't really know where to start. Taking the single request from Wordpress and then send it to the server it's easy, I only have to insert manually the single necessary data and it's done, but taking all the info from the URL (to do the GET request) and send everything processed correctly it's difficult for me.

I learned how to do GET requests from Wordpress, so with this URL I take all of my orders of the day: https://example.com/wp-json/wc/v3/orders?consumer_key=ck_xxx&consumer_secret=cs_xxx&modified_after=2024-06-25T00:00:00&modified_before=2024-06-26T00:00:00&status=completed

This return a JSON file with a lot of information, in which I have to take only a few fields, like line_items which is an array that contains all the data about the products of an order.

I'm sorry that I can't share with you the entire JSON file (because of sensitive data inside) but I try to explain to you with an example.

For example: The URL returns ten orders, which are incremental ids from 0 to 9. In each of these ids (for example I take the id 0, so the first order) there are info about that specific order such as id_order, status (which of course is always complete), currency, total, etc etc and the one the I need is line_items, which in turn contains 3 ids that are the products ordered. So the id are from 0 to 2. In the product id (in this case I take the first product, so the id 0 again), I have to take this attributes: ean, name (which is the name of the album), product_id, quantity and sku.

To be more clear I have something like that (I put in bold the attributes I'm interested in):

[
  {
    "id": 44412,
    "parent_id": 0,
    "status": "completed",
    "currency": "EUR",
    "date_created": "2024-06-21T14:25:07",
    "date_modified": "2024-06-21T14:57:54",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "6.50",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "75.40",
    "total_tax": "0.00",
     ...
     ...
    "**line_items**": [
      {
        "id": 59401,
        "**name**": "albumName",
        "**product_id**": 38515,
        "variation_id": 0,
        "**quantity**": 1,
        "tax_class": "",
        "subtotal": "68.90",
        "subtotal_tax": "0.00",
        "total": "68.90",
        "total_tax": "0.00",
        "taxes": [],
        "**sku**": "SEV53",
        "price": 68.9,
        "parent_name": null,
        "**ean**": eanOfTheAlbum
      }
      ... and here i have other data for other products, I show only the first one, so the one with id 0 in line_items, but I have to take the bold attributes for every products ...

    ],
  }
]

Next, I have to do a POST request to the server sending all of this data (The POST request it's easy per se, just like the GET request, I can do a single one but my problem is sending more than one data at a time).

The problem is, how can I write a script that does all of this automatically for example every day at 23:00 (or 11 pm)? I don't know where to start, if all of this can be done in Wordpress or not, and if not what's the best programming language and something like that.

Thanks in advance to anyone who answer this question and sorry for my english.


Solution

  • Assuming you're on a Linux system, you can do the JSON parsing in a bash script with jq. To create an array of objects with only the desired fields you can use map, which would look something like jq '.line_items | map({name, product_id, quantity, sku, ean})'. Then, after modifying the JSON so that it conforms to your API schema and adding credentials, you can pipe the result into curl --json @- <api_endpoint> (see here). To automate execution, you can use cron/anacron. Your crontab would look like 0 23 * * * /path/to/shell/script if you wanted to run the script at 23:00 every day.