Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2001
    Location
    Chicago, IL
    Posts
    1,953

    avoid timeout in php

    Without changing the PHP timeout, I need a way to refresh my search/check of the following code.

    This is what it does. Opens mysql and gets all the orders that are maked a shipped(where ID is =4). It then takes the tracking number held in the database and checks its status(via usps's website tracking) Then searches that page for the status of delivered. If it is delivered, it changes the database to reflect delivered.

    My issue is because it has to wait for USPS's website to respond, if there are too many queued up, it will time out.

    One solution was to just run it again. But this is under the assumption that enough were changed from shipped to delivered(thus not being checked). Obviously this does not work if not enough change status.


    I have seen other programs that refresh the browers and continue to work, but I can not find any code or what to search for exactly to get that effect.

    I am not looking for someone to write the code or anything, but a link to examples of what I am looking for OR search terms that may help. Code is below


    PHP Code:
    $query "SELECT * FROM orders_status_history WHERE orders_status_id = '4'";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);

    echo 
    "<center>Database Output</center></b>";


    $i=0;
    while (
    $i $num) {
    $status_ID=mysql_result($result,$i,"orders_status_id");
    $tracking_number=mysql_result($result,$i,"track_id3");
    $history_ID=mysql_result($result,$i,"orders_status_history_id");
    $orders_ID=mysql_result($result,$i,"orders_id");


    if(
    $tracking_number!= null)
    {
        echo 
    "<br>";
    echo 
    $orders_ID;
    echo 
    "&nbsp &nbsp &nbsp ";
    echo 
    $tracking_number;
    echo 
    "&nbsp &nbsp &nbsp ";


    $url_post_tn="http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum=";
    $url $url_post_tn.$tracking_number;
    echo 
    $url;

    $data file_get_contents$url );

    if (
    strlen(strstr($data,'Status: <span class="mainTextbold">Delivered</span>'))>|| $tracking_number=='9101785091401226548236') {
    echo 
    "&nbsp &nbsp &nbsp DELIVERED";
    mysql_query("UPDATE orders_status_history SET orders_status_id='3' WHERE orders_status_history_id='".$history_ID."'");
    mysql_query("UPDATE orders SET orders_status='3' WHERE orders_id='".$orders_ID."'");
    }
    else
    {
        echo 
    "&nbsp &nbsp &nbsp Not Delivered";
        }

    }
    $i++;
    }

    mysql_close(); 
    Chicago Electronic Cigarettes: Tobacco Free, Smoke Free. 3 E-Cig Models, 11 flavors, and accessories.
    http://www.chicago-ecigs.com

  2. #2
    I don't have enough time to read the whole code, but, can't you just make a cache where you store the data from USPS?, I mean, they don't update the shipment status that fast, I believe that, if you run an update script that access the site, and when someone comes to your site, they just get the status straight from the cache.
    If you create the script carefully, it can run "incrementally", so that it will only update the "update needing" entries, and thus, never hit the php timeout.

    Off course, this script should run from the command line, maybe through a cron job.

  3. #3
    Join Date
    Jun 2001
    Location
    Chicago, IL
    Posts
    1,953
    I actually do have a cron version. It runs once a day.

    I am more of a hacker when it comes to programs. I write very basic scripts for what I need and hack more complicated ones to better suit me. But this has served me well for the last 10+ years over various languages. Really adding this is more of expanding my knowledge as opposed to need it done(mainly why I request links for what I am looking for.)

    I also would like to integrate it into the zen cart admin and get status reports via that, which is also another reason I want this. I plan to later have it send back details such as why it hasent been delivered yet(usps is fickle about their reporting at times and wont always say Status: Delivered when its been delivered, they have weird messages and this would be a simple way to report them rather than daily email updates.

    "If you create the script carefully, it can run "incrementally", so that it will only update the "update needing" entries, and thus, never hit the php timeout.
    "

    Are you referring to adding another field and saying for example when it was last checked?(ie if it had to refresh it would know it just checked that entry a minute ago?)

    Thanks for your help
    Chicago Electronic Cigarettes: Tobacco Free, Smoke Free. 3 E-Cig Models, 11 flavors, and accessories.
    http://www.chicago-ecigs.com

  4. #4
    Are you referring to adding another field and saying for example when it was last checked?(ie if it had to refresh it would know it just checked that entry a minute ago?)
    Yes. And, in the event that the script "hits" the timeout, next time it runs, it will skip already updated entries, and just continue. This kind of script can be ran every, say, 5 minutes or so. I would do the script in perl, instead of PHP (I like perl the most for scripting).

  5. #5
    Join Date
    Jun 2001
    Location
    Chicago, IL
    Posts
    1,953
    Thanks again, I have been trying to avoid adding much to the database but I think your solution sounds best.

    I like perl too and thats what I started with many years ago for web scripts. But since zen cart uses all php and if I want to interface it with zen cart, and add extra functionality later, I think it makes it easier with php.
    Chicago Electronic Cigarettes: Tobacco Free, Smoke Free. 3 E-Cig Models, 11 flavors, and accessories.
    http://www.chicago-ecigs.com

  6. #6
    Join Date
    Mar 2009
    Posts
    2,218
    Yes, the underlying problem is that your environment limits the execution time of programs to something that is not long enough for your needs.

    So: just run the program more often; and modify order_status_history to include a record of when you last queried each order (default centuries ago); and modify the query to select a maximum of say ten rows, ordered by last-queried_time (so you always query the ones you've least recently queried.)

    Then run the query every five minutes or whatever.

    (Bear in mind that it's possible for cron to fire off a second batch before the first is complete. If the code can't handle that, you'll need to create a locking mechanism)

    The important thing is the code should be robust, so if your server goes down for a day, it should recover OK, just working through its backlog in batches of 10 every five minutes.

    Oh: and I like the idea of limiting the number of queries to 10 or so.

    The reason is, if you were to fire off a hundred queries, and time out after say 20, the USPS server still has to process the other 80, even though the results will not be used. If lots of people did that, the USPS server could get saturated with useless work. Restricting the number of queries to a number you can handle is neighbourly.
    Last edited by tim2718281; 11-07-2009 at 06:01 PM.

  7. #7
    Join Date
    Jan 2008
    Location
    England
    Posts
    573
    Might also want to take a look at USPS API:
    http://www.usps.com/webtools/

Similar Threads

  1. Replies: 13
    Last Post: 10-13-2009, 12:53 PM
  2. Replies: 6
    Last Post: 09-16-2008, 04:24 PM
  3. Replies: 3
    Last Post: 08-08-2007, 04:44 PM
  4. Site Turn Networks - Avoid! Avoid! Avoid!
    By Wraith333 in forum Web Hosting
    Replies: 1
    Last Post: 04-01-2004, 10:37 PM
  5. Timeout in IIS
    By andy18 in forum Hosting Security and Technology
    Replies: 0
    Last Post: 01-03-2003, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •