PHP Callback Function Help

Discussion in 'PHP' started by x11joex11, Jan 17, 2008.

  1. #1
    Does anyone know how to use CURLOPT_READFUNCTION? Google can't seem to find anything... Thanks in advance!! (Really need help, stuck on job :()

    I assumed I would do code like this.

    curl_setopt($curl, CURLOPT_READFUNCTION, 'read_function');
    PHP:
    and for it's function

    //CURL CALL BACK FUNCTIONS----------------------------------------------------
    function read_function($ch,$string)
    {
        $length = strlen($string);
        echo "Received $length bytes<br />\n";
        return $length;
    }
    PHP:
    but nothing happened or showed... I then tried write CURLOPT_WRITEFUNCTION but that was just weird, while it outputed data it seems like it screwed up my return transfer, not sure why.
     
    x11joex11, Jan 17, 2008 IP
  2. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #2
    remove '' from function name
    curl_setopt($curl, CURLOPT_READFUNCTION, read_function);
    PHP:
    try this
     
    sunnyverma1984, Jan 17, 2008 IP
  3. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unfortunatly didn't seem to make a difference, The code is here

    http://dnfinder.net/rentacoder/alib...ompanies.html&step=3&debug=0&display=0&test=0

    (In advance just a tool to grab counts of categories)

    Notice how when it gets to Using Company List Links to get Company Page Links (1995), it just sits there and stops that is where it should be running the callback function. without it the script just stops as you can see, your browser till terminate it=(. From what I've been told on the forum this has to do with the browser closing the connection. I've tried setting the headers keep-alive:100000000 and connection:keep-alive, no luck, and I don't want to do ignore_user_abort as that would defeat the purpose of showing the user output, it seemed my only option was callback functions to constantly feed data to the browser.

    Any ideas?

    Think this has any promise? http://us.php.net/apc

    (I've been testing using fireFox)
     
    x11joex11, Jan 17, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    If you're looking to end the script after the browser kills connection, you could try using http://uk.php.net/manual/en/function.register-shutdown-function.php

    Hope this helps,

    Jay
     
    jayshah, Jan 17, 2008 IP
  5. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no I want the browser to NOT kill the connection :(, thanks for trying to help though, anyone else got any ideas?

    the whole point is that it will continue to show data.
     
    x11joex11, Jan 17, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Hi,

    There is no way to stop the browser from killing the connection. You've already declared you don't want to use ignore_user_abort, but why not (then) log the data to a file, and display it to the user once they return?

    Jay
     
    jayshah, Jan 17, 2008 IP
  7. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ah damn I was afraid of that, I can do that but then how do I track the progress of it happening? The events I run can take literally 2 hours.

    I guess I could just keep attempting to open the logfile for reading to show progress perhaps. My other concern now is 'how do I stop the script', I'm scared if I set it on some loop and I made an error in code and need to test it, there is no way to abort it now!'.

    Also I think I need the CURLOPT_WRITEFUNCTION, I was reading up on it more on php.net and clarified it as this.

    Which is strange, because when I use that callback option, my program falls apart, not sure why. My guess is maybe my callback function the 'read_function' one, which I'm now using CURLOPT_WRITEFUNCTION for is editing the resource and data by the time it's done with the transfer. I'm not sure but I will be doing some testing.
     
    x11joex11, Jan 17, 2008 IP
  8. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #8
    I'm not exactly sure what you mean. You could create a file saying like running.txt (and log all data to it AS WELL as outputting it) and remove it once the script finishes (using the register_shutdown_function, or putting it at the end of the code). Once the script is revisited, it will just fread() an infinite loop of running.txt file.

    Hope this helps,

    Jay
     
    jayshah, Jan 17, 2008 IP
  9. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Sounds like an interesting plan, I guess I'll start to code for this then.
     
    x11joex11, Jan 17, 2008 IP
  10. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Actually wait I'm still scared, how do I 'stop' the function from working? I just realized, lets say I have an infinite loop the script is in. When the user goes to the page that makes an instance of the php code that is meant just for that client (yes, no?), and I assume there is no way to make another php instance stop 'that' instance.

    I just realized that sounds confusing, I'll try and word it differently. When a user goes to a php page lets say I have a loop while(true) do some stuff, and of course it will always be true so it will never end, if ignore_user_abort() is on, then how do I stop it? Because ignore_user_abort() will keep the function going despite the browser disconnection, it will probably destroy my server by taking up all the processing power.

    I'm not sure of how Register_shut_down function works?

    Was reading up on it here but still very confused.

    http://us2.php.net/manual/en/function.register-shutdown-function.php
     
    x11joex11, Jan 17, 2008 IP
  11. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Hi,

    You will have to write something that allows you to 'stop' it. Like:

    
    if (file_exists('kill.txt')) exit();
    
    PHP:
    Therefore, just create kill.txt and the script will terminate.

    register_shutdown_function(function) works by executing that function just before the script is about to end.

    
    <?php
    
    register_shutdown_function("bye");
    
    function bye(){
        echo "Goodbye";
    }
    
    echo "Some stuff <p>";
    ?>
    
    PHP:
    Will output:

    (untested).

    Jay
     
    jayshah, Jan 17, 2008 IP
  12. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Just tested and edited code slightly. Tested and works.

    HTML Output:
    Some stuff <p>Goodbye
    Code (markup):
    Jay
     
    jayshah, Jan 17, 2008 IP
  13. x11joex11

    x11joex11 Peon

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    some good ideas there, I'll attempt to start re-coding my project from scratch with this in mind, thanks a lot!
     
    x11joex11, Jan 17, 2008 IP
  14. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #14
    For future reference, if you cannot terminate the PHP script (for whatever reason), just restart Apache.
     
    jayshah, Jan 17, 2008 IP
    x11joex11 likes this.