0

For an iOS Push Notification server, I am implementing a web service that checks a feed on the net for a particular price.

Therefore I need my PHP to keep checking a price (every 20 seconds or so) and check values.

I was wondering (forgive my ignorance I just started with PHP today) is the way people do this a cronjob? Or is there some special way to fire a php script that runs until it's killed and repeats a task?

Thanks! John

Woodstock
  • 22,184
  • 15
  • 80
  • 118

2 Answers2

1

That is possible by setting up a cron jobs on your server.

  1. Login to your web hosting e.g cpanel create a new cron job and add the path to the php file that you want to run. e.g php /home/[your username]/public_html/rss/import_feeds.php. There is field where you can input the number of minutes would you like the php script to run.

Run a PHP file in a cron job using CPanel

Community
  • 1
  • 1
Roseann Solano
  • 762
  • 2
  • 8
  • 13
1

If PHP was your preferred route, a simple script such as the following can be set to run indefinitely in the background (name this grabber.php):

#!/usr/bin/php
<?php
do {
    // Grab the data from your URL
    $data = file_get_contents("http://www.example.com/data.source");

    // Write the data out somewhere so your push notifications script can read it
    file_put_contents("/path/to/shared/data.store", $data);

    // Wait and do it all over again
    sleep(20);
} while (true);

And to start it (assuming you're on a unixy OS):

$ chmod u+x grabber.php
$ ./grabber.php > /path/to/a/file/logging/script/output.log 2>&1 &

That & at the end sends the process to run in the background.

PHP is probably overkill for this however, perhaps a simple bash script would be better:

#!/bin/bash
# This downloads data and writes to a file ('data-file')
doWork () {
    data=$(curl -L http://www.example.com/data.source)
    echo $data > data-file
    sleep 20
    doWork
}

# Start working
doWork

$ chmod u+x grabber.sh
$ ./grabber.sh > /path/to/logger.log 2>&1 &
jgauld
  • 639
  • 5
  • 10
  • great stuff! thanks! I need PHP to set up a secure connection to the apple push notification server and send a json string. sockets etc... can't use bash. – Woodstock Aug 13 '13 at 20:58