i have a small PHP Cron Job that runs every day get a file from an API and save it to a static file.
file_put_contents("api.json", fopen("http://example.com/api", 'r'));
The content of this JSON looks like that:
{
recipes: [
{
id: 30476,
title: "Selfmade Chicken Nuggets",
...
},
{...} ]
}
My issue: I would like to create a "recipes of the day" logic.
Therefore i would like to create an extra array with recipes per day.
In best case I would like to have something like this:
Step 1: Create a "remaining recipes array" that contains all recipes
Step 2: Get 3 recipes per day from the remaining recipes array and put them in some kind of "recipes of the day"-array
Step 3: If the remaining recipes array is empty or don't have 3 elements, refill it from recipes
I already have that logic in my Javascript client:
let fullRecipeList = await this.appData.getRecipeList();
let recipesOfTheDay = await this.appData.getItem("recipesOfTheDay");
let recipesOfTheDayValidUntil = await this.appData.getItem(
"recipesOfTheDayValidUntil"
);
let remainingRecipes = await this.appData.getItem("remainingRecipes");
if (!remainingRecipes || remainingRecipes.length < 3) {
remainingRecipes = this.shuffleArray(fullRecipeList);
}
if (
recipesOfTheDay &&
moment(recipesOfTheDayValidUntil).isSame(new Date(), "day")
) {
this.recipeList = recipesOfTheDay;
} else {
recipesOfTheDay = remainingRecipes.splice(0, 3);
this.recipeList = recipesOfTheDay;
await this.appData.setItem("remainingRecipes", remainingRecipes);
await this.appData.setItem("recipesOfTheDay", recipesOfTheDay);
await this.appData.setItem(
"recipesOfTheDayValidUntil",
moment().startOf("day")
);
}
Is it possible to create that kind of logic in my server side cron job?
How would the code look like? I am quite new to the whole PHP world :)