12 Replies - 705 Views - Last Post: 04 March 2013 - 03:22 AM Rate Topic: -----

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

time_sleep_until and filesize not working?

Posted 02 March 2013 - 05:26 AM

i use this php code to grab a web page. i use time_sleep_until but it does not seem to even exist?
filesize comparison seems to be ignored too? (i have tested and it did give correct file size.) why do the curl statements execute every minute even when the file size has changed?

while(1)
{
	$simpledate=date('Y-m-d H:i:s');
	$date=strtotime($simpledate);
	$year=date('Y',$date);
	$month=date('m',$date);
	$day=date('d',$date);
	$hour=date('H',$date);
	$minute=date('i',$date);
	$sec=date('s',$date);
	$tomorrow=mktime(0, 0, 0, date("m"), date("d")+1, date("y"));

    if ($hour>23) {
      $str=$tomorrow.' 9:10:30';
      $timestamp=strtotime($str);
      time_sleep_until($timestamp);
    }
    $oldsize=filesize('todays11');

	$myURL ="http://example.com/";
	$ch = curl_init();	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_URL, $myURL);
	curl_setopt($ch, CURLOPT_HTTPGET, TRUE); 
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	$content=curl_exec($ch);
	
	$myfile='todays11';
	$f=fopen($myfile,'w') or die ('cant open file');
	fwrite($f,$content);
	fclose($f);
        $newsize=filesize($myfile);
        if(floor($newsize/100)==floor($oldsize/100) || $newsize<10) { // try again
	  sleep (60);

        } else {
        if($minute<50)$minute=floor($minute/10)*10+10;
        else {$hour=$hour+1; $minute=00;}
        $str=$year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':30';
        $timestamp=strtotime($str);
        time_sleep_until($timestamp);
       }
}

This post has been edited by g37752: 02 March 2013 - 03:31 PM
Reason for edit:: please use example.com or example.org for generic domains


Is This A Good Question/Topic? 0
  • +

Replies To: time_sleep_until and filesize not working?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 05:35 AM

View Postg37752, on 02 March 2013 - 01:26 PM, said:

i use this php code to grab a web page. i use time_sleep_until but it does not seem to even exist?

on Windows you need at least PHP 5.3

maybe a cronjob is an option for you?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,638
  • Joined: 12-December 12

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 06:09 AM

Also, the hour extracted from date('H') is from 0 to 23, so it will never be greater than 23. The docs
Was This Post Helpful? 2
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 08:57 AM

View PostDormilich, on 02 March 2013 - 07:35 AM, said:

View Postg37752, on 02 March 2013 - 01:26 PM, said:

i use this php code to grab a web page. i use time_sleep_until but it does not seem to even exist?

on Windows you need at least PHP 5.3

maybe a cronjob is an option for you?


Otherwise known as a scheduled task on Windows.
Was This Post Helpful? 0
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 01:09 PM

View PostDormilich, on 02 March 2013 - 05:35 AM, said:

View Postg37752, on 02 March 2013 - 01:26 PM, said:

i use this php code to grab a web page. i use time_sleep_until but it does not seem to even exist?

on Windows you need at least PHP 5.3

maybe a cronjob is an option for you?


i did not think cronjob has the ability to 'sleep until', i will then have to have some 100 lines of almost duplicate actions?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 03:01 PM

The point of a cron job is to schedule execution. This means that the code would have no reason to sleep. It would simply run when the cron job determines that it is time to run.
Was This Post Helpful? 1
  • +
  • -

#7 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 03:28 PM

View PostCTphpnwb, on 02 March 2013 - 03:01 PM, said:

The point of a cron job is to schedule execution. This means that the code would have no reason to sleep. It would simply run when the cron job determines that it is time to run.


yes, but cronjob is not smart enough to judge if the webpage has been updated. thats what i am trying to do using filesize.

i have trouble finding the logical error of my if statements. the curl statements execute every minute even when the filesize has changed?

This post has been edited by g37752: 02 March 2013 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: time_sleep_until and filesize not working?

Posted 02 March 2013 - 04:14 PM

The curl executes every time the script is run. Your else is setting variables that are lost when the script ends.
Was This Post Helpful? 0
  • +
  • -

#9 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: time_sleep_until and filesize not working?

Posted 03 March 2013 - 12:37 AM

solved: clearstatcache();
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: time_sleep_until and filesize not working?

Posted 03 March 2013 - 04:19 AM

I still bet you're doing this wrong.

The right way to do this -- provided the web server is set up properly -- would be a cronjob/scheduled task. The script would on first run issue a GET request to the page and save the contents of the Last-Modified header somewhere. Subsequent requests would start with a HEAD request to the page which will retrieve only headers describing the page, including the "Last-Modified" header. It would check the returned value of this header against the saved value, and if the new value is more recent, then issue the GET request to retrieve the rest of the data and change the stored Last_modified time.

Lather, rinse, repeat.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: time_sleep_until and filesize not working?

Posted 03 March 2013 - 08:22 AM

Jack is right, but of course this all assumes that the page is not dynamic. In that case it can be (but might not be) different every time it's accessed.
Was This Post Helpful? 1
  • +
  • -

#12 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: time_sleep_until and filesize not working?

Posted 04 March 2013 - 02:42 AM

View PostJackOfAllTrades, on 03 March 2013 - 04:19 AM, said:

I still bet you're doing this wrong.

The right way to do this -- provided the web server is set up properly -- would be a cronjob/scheduled task. The script would on first run issue a GET request to the page and save the contents of the Last-Modified header somewhere. Subsequent requests would start with a HEAD request to the page which will retrieve only headers describing the page, including the "Last-Modified" header. It would check the returned value of this header against the saved value, and if the new value is more recent, then issue the GET request to retrieve the rest of the data and change the stored Last_modified time.

Lather, rinse, repeat.


ok, but thats not exactly what i want to do. i would have to send requests way too often.
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: time_sleep_until and filesize not working?

Posted 04 March 2013 - 03:22 AM

You can schedule the cronjob/scheduled task for any time interval you want. And using the HEAD request makes the actual request as small as possible.

Whatever. Just pointing out how a professional would do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1