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

Posted on Friday, August 21st, 2009    •    1 Comment    •    655 views
WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.

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.
"; }
Share and Enjoy:
  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Facebook
  • Twitter
  • Reddit
  • MySpace
  • Google Bookmarks
  • MisterWong
  • Ping.fm
  • Slashdot
  • RSS
  • Live
  • LinkedIn
  • email

1 Response to “Get content of an URL (useful for multi scheduled tasks)”

Leave a Reply