Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    cURL Callback in a Class?

    I'm writing a class that uses cURL to download a file and stores the contents in a variable. I'm using the CURLOPT_WRITEFUNCTION option to set a callback function, but the function is a method in the class (and the function name must be passed as a string), so I don't know how to call it. Any ideas?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I've never tried this with the curl, but when I'm passing data in classes I use call_user_func[_array]. The parameter is normally a string representing the function name, but it overloaded to handle an array indicating the object and the method:
    PHP Code:
    call_user_func_array(array($this'myMethod'), $params); 
    Try using the array($this, 'method') with the setopt to see if it will allow you to use the class method combination.
    I don't know if this will work, but its worth a shot. Post back you're results, I'm a little curious :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I forgot about call_user_func, that is a similar situation. So yep, that works! Thanks! I now have:
    PHP Code:
    curl_setopt($chCURLOPT_WRITEFUNCTION, array($this'remoteread_curl_writefunc')); 
    to set the callback.

    And it just so happens that at the same time you replied, I found this to back it up and provide a bit more information on it.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    I've never tried this with the curl, but when I'm passing data in classes I use call_user_func[_array]. The parameter is normally a string representing the function name, but it overloaded to handle an array indicating the object and the method:
    PHP Code:
    call_user_func_array(array($this'myMethod'), $params); 
    Try using the array($this, 'method') with the setopt to see if it will allow you to use the class method combination.
    I don't know if this will work, but its worth a shot. Post back you're results, I'm a little curious :P
    first argument of the call_user_func_array must be a callback function, don't work with a array. Maybe changing your idea to something like this could work:

    PHP Code:
    function callbackwrap($obj$para){
       
    $obj->method($para);
    }
    call_user_func_array("callbackwrap",array($this$params)); 
    regards

  • #5
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    One more quick question: I'm noticing that the callback is called before any other code is executed. For example, I create a new object of the class I mentioned in my first post, call my method to read a remote file with cURL, then echo a variable (which is set to the contents of the remote file by the callback method). Strangely, this variable is properly set with the contents of the file immediately after calling my read method.

    Code:
    PHP Code:
    $reader = new remoteReader("http://aw.pehjota.com/besaid/map/zones.xml");
    $reader->remote_read();
    echo 
    $reader->contents
    $reader->contents is the contents of the remote file, and it is populated by my callback method which is set in $reader->remote_read(). So is it normal for the callback to be called so quickly like this?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by oesxyl View Post
    first argument of the call_user_func_array must be a callback function, don't work with a array. Maybe changing your idea to something like this could work:

    PHP Code:
    function callbackwrap($obj$para){
       
    $obj->method($para);
    }
    call_user_func_array("callbackwrap",array($this$params)); 
    regards
    The PHP api is somewhat difficult to track things down like this, but using array($classname, $method) or array($object, $method) are considered legal callback types: (http://ca3.php.net/manual/en/languag...types.callback):
    A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

    Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.

    What pisses me off about PHP is I cannot typehint my own callback, that would rock:
    PHP Code:
    [public] function mySorter(callback $uCallback, array &$stack)

    I really with that they'd open up the callback for a typehint like they did with the array. I'd even settle for a C style function pointer.


    Looks like he got it working though, so that rocks.

    Edit:
    Snuck a post in there
    If its anything like the call_user_func methods, it is called on the executing line. I'm not certain how the curl methods work, but given that they are procedural based I would suspect the method to be called as soon as you've run the command for it. This does execute like a !fork call in perl, so you're program halts while it finishes executing the file.
    If you're code is working though, I wouldn't be at all concerned about it. Sadly, PHP doesn't really have a strong threaded mechanism to it, so you can't really tell something to wait until another thread is complete. Best you've got is a tick function, and I've never so much as tested it before.
    Last edited by Fou-Lu; 08-10-2008 at 02:27 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    oesxyl (08-10-2008)

  • #7
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    If its anything like the call_user_func methods, it is called on the executing line. I'm not certain how the curl methods work, but given that they are procedural based I would suspect the method to be called as soon as you've run the command for it. This does execute like a !fork call in perl, so you're program halts while it finishes executing the file.
    If you're code is working though, I wouldn't be at all concerned about it. Sadly, PHP doesn't really have a strong threaded mechanism to it, so you can't really tell something to wait until another thread is complete. Best you've got is a tick function, and I've never so much as tested it before.
    I wouldn't be concerned about it, except this is for a professional application to run in many different environments, from the average home PC to a production server. So I need to make sure everything works the same everywhere.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    The PHP api is somewhat difficult to track things down like this, but using array($classname, $method) or array($object, $method) are considered legal callback types: (http://ca3.php.net/manual/en/languag...types.callback):
    I know about object and methods that works but not about this array syntax,
    thank you, is good to know,

    regards

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I hadn't ever used the user_func calls in an object before I had created my collections. Since it had contained so many method with overloads I needed to be able to pass it in. Tried to pass it in as an array, and hurray it worked . I was pleased, and haven't forgotten about them since!

    As for you're environment, it won't matter if its on a server versus a home pc. Since the code halts while the curl does its things, its just a matter of time. You may need to increase you're timeout, but I doubt that would pose a huge problem either. Only consider doing this if you are experiencing timout problems due to the communication with the external server. This is actually an advantage of non-threaded programs. Generally the next step cannot occur until the previous step has been complete (and returned a result in this example). This is why animation in something like Java is so choppy without threading. Threaded programs are a pain because you need to lock all over the place, and handle you're locks of something goes wrong in a thread (ie, a thread experiences an exception or is deadlocked).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I noticed a difference between Windows and Linux in the order callbacks are called in a different extension, so I'm being careful to prevent any other differences from slipping past, that's all.

    Anyway, thanks again for the help!

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    NP.
    If you've had previous problems with PHP installations in different OS', you should test it out to make certain that it will work. I myself haven't noticed any differences between apache on either windows or Linux when it comes to executing code (except with time zones or languages due to windows non-thread safe nature). There may be differences in IIS but I can't confirm - I don't use IIS unless its for .net, php just runs so much better on apache.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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