HTMHell
HTMHell

Reputation: 6016

Is it possible to use a cURL callback function inside a class?

I have a function inside a class that runs a cURL. In the cURL I'm using:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');

The progress function is inside the class, and using its properties and its methods. ($this->property, $this->method())

When I run the script, I get this error message:

Warning: curl_exec(): Cannot call the CURLOPT_PROGRESSFUNCTION in ...

So the question is: Can I do that? And if so, how?

Edit: I have tried to change the code to this:

$ref =& $this;
curl_setopt($ch,
    CURLOPT_PROGRESSFUNCTION,
    function($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($ref)
    {
        $ref->progress($resource, $download_size, $downloaded, $upload_size, $uploaded);
    }
);

But I'm still getting the same error.

Update: The solution of @Ohgodwhy is working, just make sure that your callback function doesn't have any errors.

Upvotes: 4

Views: 2460

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50797

Instead, pass in an array with a reference to the object as the first parameter and the function name as the second.

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'method'));

Upvotes: 10

Related Questions