Get content of an URL (useful for multi scheduled tasks)

I have a client’s API which located on different Domain. We are asked to access it by its URL with corresponding GET parameters. file_get_contents seems to be a simple way to make it works.

$api_url_result = file_get_contents($api_url);
if ($cron_daily_url_result)
    echo ($api_url_result );
else
    echo "Unable to access.
";

With such implementation, it can also be used in cronjob where it doesn’t need to create a new cronjob for every single task that to be executed on same time or interval. For example, if there are 3 tasks to be done on every midnight 12am, then create a PHP (midnight_cron.php) which groups 3 tasks into array, loop through it with above implementation. And lastly configure the cronjob for that PHP (midnight_cron.php).

$cron_midnight_urls = array(
			"http://www.domain.com/task1.php"
			, "http://www.domain.com/task2.php"
			, "http://www.domain.com/task3.php"
			);

foreach($cron_midnight_urls as $cron_midnight_url) {
    echo "$cron_midnight_url
";
    $cron_midnight_url_result = file_get_contents($cron_midnight_url);
    if ($cron_midnight_url_result)
        echo ($cron_midnight_url_result);
    else
        echo "Unable to access.
";
    }

One thought on “Get content of an URL (useful for multi scheduled tasks)