How to fetch (poll) data periodically from an rss feed - php

I have a rss feed (xml) that is updated frequently. I need to send notifications to APNS if new update is avaliable on rss feed (xml). So far I know I can parse xml with php and send the result to APNS with my local Apache Server on Mac (MAMP).
But I do that by simply entering php xxx.php command on terminal window.The File xxx.php first parses the xml file and then sends the results to the APNS server.
My question is
How can I periodically run this php file on a server?
Do I need a Virtual Private Server?
If so what is the code or function for running a php file continuously or every 10 mins ?

To run a file periodically you have to manage cron jobs, I wanted to describe what you have to do but I found a good profound article, It's worthy to have a look on it.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

You can use cron on a linux/mac server to run periodically any command (for example: php xxx.php)
I) In a command prompt you can edit the crontab with: crontab -e
II). At the end you add a line for every script you want to run periodically. Each line must follow this pattern:
`minute` `hour` `day of the month` `month` `day of the week` `command`
For example if you want to run the script every hour you add this line:
* */1 * * * php xxx.php
III) Then exit with CTRL+X and save.
More examples on Wikipedia.

What you need is a cron job: See this Wikipedia entry.
Check to see if your host allows you to run cron jobs, and if not -- consider contacting them to see if they can set it up for you.

Related

Hitting URL every minute in background

I want to hit a URL of my application through cron job. I have done the following things:
1) Opened the terminal
2) Did crontab -e which gives me an editor that allows me to put statements
3) Pasted the URL that needs to be hit after specified interval of time:
curl -s http://www.example.com/controller/function_to_execute
This cron job will run every minute
4) Saved the cron and again on terminal did crontab -l, and I could see my cron
This scheduler isn't working, don't know why. I tried curl on the terminal directly expecting some output, but after 5 minutes it gives me the result
curl: (7) couldn't connect to host
If I hit my URL on browser directly then my job gets successfully executed!
Have I made some mistake while making entry in the cron?
First you need to create the php script and run the script using cronjob
1)Open terminal and type crontab -e
2)Edit the file and write the following code to run the php script in background
*/1 * * * * php /yourpath/yourphpfile.php
3) Create yourphpfile.php and write the code to hit the url
you have to execute and test the yourphpfile.php before doing cronjob
Whoops!..tried to just curl www.google.com and gave the following output in form of HTML tags "302 Document has moved", Also I am not able to ping the application itself from the terminal but it is accessible publicly
So cron job getting executed is out of question :P

run a php page once a day using NAS synology

have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.

sending email periodically through php script

I am working on an automatic monitoring system. I have made a .NET application which have MySQL database. For this I developed a normal ADMIN Panel where admin can log in and get necessary reports coming from various queries fired on the database. There is also a "summary Report" in the panel which is just the rough weekly summary. Now What I want is, I want this report (all text) to get sent automatically to some email "xxxxx#xxx.com" with a seven day period. I have used some PHP scripts previously to send email on submit button click. Like the one below.
<?php
if(isset($_POST['isPost']))
{
$header="From:".$_POST['customer_mail']."\r\nName:".$_POST['name']."\r\nCity:".$_PO ST['city'];
$subject = $_POST['title'];
$message="From:$_POST[customer_mail]\r\nName:$_POST[name]\r\nPhone:$_POST[city]\r\n \r\n\r\n\r\n".$_POST['details'];
$to = 'xxxxxxxxx#xxx.com';
$send_contact=mail($to,$subject,$message,$header);
if($send_contact)
{
echo "<h6 style='text-align:center;color:green'>Sent Successfully</h6>";
}
else
{
echo "<h6 style='color:red'>Error sending e-mail'</h6>";
}
}
?>
this is normal mail sending script which works fine. But for the above purpose I want, Can anyone help me to set this action periodically and automatically or point me in the right direction on how to do this. I have googled for such php script but no satisfied results.
~ Regards
You can do this with cronjobs.
A long running process which executes commands at given times / dates / intervals.
Depending on what system you are, there are different methods.
If you have the script on a webserver someone is running for you / Webhost service
Ask the system administrator to run the script with a cronjob. Or search for any help documentation if you can setup this yourself in any admin-panel. Ask your webhoster /system admin for more information.
If you have the script on your own local machine:
UNIX
Try reading about crontab on how to run the php script, or any script for that matter.
For example type crontab -e and add the line below in your crontab, the cronjob will run your script every hour:
00 * * * * /usr/local/bin/php /home/Low-pointer/myscript.php
Here is some more information if you want to play with the intervals
Windows
If you use Windows you can use scheduled tasks to run the php command.
For example add the following command to the scheduler: C:\wamp\bin\php\php.exe -f C:\wamp\www\my_script.php
You can also use web cron services (Google it) these are online services which run a page (for example on your webserver) on designated times. For free or paid.
You can use Cpanel to send schedule emails through the cronjob(if you are using it).
Once you open cpanel theere would be crontab system.
Add your current code to a file(xx.php) and add this command to crontab in cpanel ,
/usr/bin/php -q /home/public_html/xx.php
like everyone have already said you must use cronjob to make your task.
I assume you use a Linux OS as your production environment.
So you need:
1) A php endpoint ( eg. www.mywebsite.com/emailsend.php ) OR a CLI php script that when called send the email.
2) The correct crontab rule
Here below an example of a simple shell script ( mailsend.sh ) that call an endpoint using CURL and save an html file with an eventual response given by the webserver
#!/bin/bash
curl http://www.mywebsite.com/emailsend.php -o /path/to/my/mailsendreport/"$(date '+%Y-%m-%d-%H-%M-%S')".html
To add a scheduled task to cron
crontab -e
then add a rule like below
50 23 * * 6 sh /path/to/mailsend.sh
What "50 23 * * 6" means? It means that every sixth day of the week, in the 23th hour, in the minute 50 your sh script will be called, and then your web app and the email is sent.
Once I wrote a small doc about this you can see it here
Cheers
You're looking for "cron job".
Edit: Or since it sounds like you might be on Windows, "Scheduled Tasks"

How to run a script under CRON that outputs a CSV file and is then emailed

I need to run a script that queries my DB and outputs the results as a CSV file. I have the script working to produce the CSV I now just need to figure out how to schedule this task for 2am and email the output CSV to a list of email addresses using PHP.
Any help or ideas would be appreciated.
you edit your crontab and simply schedule what you want to happen
e.g.
$ crontab -e
to edit your crontab. Add an entry to run your script
MAILTO=me#example.com
00 02 * * * cd /here && runMy2AMScript
And that's about it. your script does all the work, cron just calls it. For more information about crontab - use man crontab or man 5 crontab.
If your unclear how to schedule sending an email - that's 2 tasks scheduling (which is above) and a script that sends an email - which you can do any number of ways (such as using swiftmailer. Pick a technique and ask another specific question if you are stuck).
I don't have much idea about cron. But once I tried firing mail using cron. My php site folder is home/USER/www. So this is what I did:
1. created a txt file: cron.txt
2. placed cron required informtion : 29 16 * * * php /home/USER/www/test_cron.php
3. ( test_cron.php contains code for a dummy mail )
4. opened terminal and executed command : crontab cron.txt
and its been two month and I daily get an email on 4:29pm
Hope this information will help you.
Any help or ideas would be appreciated.
More information would be appreciated. Where do you run into problems? Is it in the scheduling? If so, do a Google search for "Scheduled tasks" in case the webserver is running Windows, or "Cronjobs" if the webserver is running Linux, Unix or BSD.
E-mail a file is rather simple if you use a library; I would suggest looking into attaching files with Swiftmailer.

How to set up a cron job via PHP (not CPanel)?

How to set up a cron job via PHP (not CPanel)?
Most Linux systems with crond installed provides a few directories you can set up jobs with:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
...
The idea here is to create a file in one of these directories. You will need to set the proper permissions/ownership to those (or one of those) directories so that the user launching the PHP script can write to it (Apache user if it's a web script, or whatever CLI user if CLI is used).
The easiest thing is to create an empty file, assign proper permission/ownership to it, and have the PHP script append/modify it.
Per example:
$ touch /etc/cron.d/php-crons
$ chown www-data /etc/cron.d/php-crons
Then in PHP:
$fp = fopen('/etc/cron.d/php-crons', 'a');
fwrite($fp, '* 23 * * * echo foobar'.PHP_EOL);
fclose($fp);
If what you're getting at is dynamically adding lots of jobs to crontab form your application, a better way to do that is manually add ONE cron job:
php -f /path/to/your/runner.php
Store your jobs that you would be adding to cron manually in a table (or one table per task-type), and then have your runner go through the table(s) every minute/hour/day/whatever and execute all the ones that should be executed at that time.
From pure PHP I will create deamon that will manage this (those) cron job(s).
how to create it:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ to start with
Finding crontab file isn't easy on shared hosting and there's no certainty that cron will read that file again while it's already running.
Actually I the best way is to use corntab command.
If you don't have access to shell you can use for example PHPShell. Try this.
Uplode a txt file via FTP with jobs in crontab fomat for example
5 * * * * /some/file/to/run.sh > /dev/null
(remember to put a newline at the end of that line)
Log in to your PHPShell and run
crontab uploded_filename.txt
Remember to change file permissions
chmod 775 uploded_filename.txt
Check your cron jobs using
crontab -l
Cheers
There is an embargo on the use of PHP to edit crontabs which has been in place since 2004. You may not be allowed to do this if you live outside of the United States, check with your local government agency.
But seriously, you could always call "crontab -" with a system call. If you need to do this for some user other than the webserver, you'll need some ssh or sudo magic. But it all seems like a bad idea.

Categories