Jump to content
xisto Community
toby

How To Use Cron Jobs To Save Two Images?

Recommended Posts

I have little knowlegde of PHP, and none/never done a cron job. How would I save two images that change every ten minutes on another website? Maybe add a timestamp and put them in a folder.

Share this post


Link to post
Share on other sites

Well i dont know if it is possible to do it but what i know is that it is a bit difficult to use cron jobs with php, may be i'm wrong.Best regards,

Share this post


Link to post
Share on other sites

I've use cron jobs to access a php script that does something, everyday. It's for my hotel management system, where the system will post the room charges automatically everyday. I didn't call the cgi version, instead i use wget to call the page locally

wget -q http://localhost/autopost.php -O - >> /home/myfolder/logs
that way, the page will run exactly the same as if it runs from a browser, and the text output is appended to the logs in myfolder.

toby, if the image you're trying to save has static url, then it would be easier. But i'm not sure of the exact way of doing it.

If the url is static, maybe you can use fopen() to open the file stream of the image, then save it to another handle to a local file(open with fopen() with mode = x) with fwrite(). Seems complicated. Another way is to use wget to download the file programatically to the temp folder, then use rename() to move that file to your desire destination and at the same time rename it to your choice

If the url is not static, things get a bit more complicated, you'll have to parse the page that holds the image manually, to get the url of the image, then continue with the above method. the parse might be able to do with DOM, but i'm not sure and never did.

The above is just a suggestion of concept, it's not proven to work. But might give you some idea of where to start. Try read the manual of those mentioned php function, the comment section might provide more hints

Good luck
Edited by faulty.lee (see edit history)

Share this post


Link to post
Share on other sites

http://forums.xisto.com/no_longer_exists/ And http://forums.xisto.com/no_longer_exists/

 

So how would the php script go? What access does wget need?

 

This is a crude method, but i just tested, and works on my side.

Save this as /home/myhome/test.php

$temp_filename = "webcam-" . date('Y-m-d-G.i') . ".jpg";system('wget -q http://forums.xisto.com/no_longer_exists/ -O /home/myhome/' . $temp_filename);echo "Done fetching " . $temp_filename . "\n";

the code first generate a temp filename from the current server time

then call wget to download the image and save as the filename generated

 

edit your crontab as

 

*/10 * * * * php < /home/myhome/test.php

EDIT: you can refer to crontab here https://en.wikipedia.org/wiki/Cron

 

you can use php or php-cgi

the output will be

 

Content-type: text/htmlX-Powered-By: PHP/5.0.4Done fetching webcam-2006-11-23-22.32.jpg

If you want to log the output of the php, just add a redirection like this

 

* * * * */10 php < /home/myhome/test.php > /home/myhome/logs

That's all. One thing though, i don't know how to remove the http header from the php output. Anyway, the rest is up to you to refine.

 

Good luck

Edited by faulty.lee (see edit history)

Share this post


Link to post
Share on other sites

If you're going to use system calls it's kind of pointless to use PHP then. I mean why bother with PHP when a simple shell script would do the job. Problems tend to rise when you're not on your own server as system calls from PHP usually are blocked (and for a good reason).Loading an image from an URL is actually not that difficult. If you have fopen wrapper enabled you can just pass the URL as parameter to imagecreatefromjpeg() (you can replace jpeg with gif or png too) function . Then just do whatever you want to the image, resize it, save it, push it to database, etc. If the wrapper is not enabled you then need to open a filehandle (just fopen(http://forums.xisto.com/no_longer_exists/) ) and read the binary to a variable using fread() and then proceed with imagecreatefrom*().EDIT: Please post if you need more detailed examples.

Edited by Hercco (see edit history)

Share this post


Link to post
Share on other sites

Hi Hercco,I do admit that i wasn't very good with linux and shell script. Just trying to give a working example. I've tested that on a linux server where i've quite a lot of rights given. Anyway, you idea seems cleaner from php point of view.

Share this post


Link to post
Share on other sites

Yes, what Hercco said is correct, because in most cases hosting providers disable the system function for security reasons, so i think that the solutions he proposes are better to use.Best regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.