Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oop

php

curl

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.

like image 540
HTMHell Avatar asked Jan 07 '23 18:01

HTMHell


1 Answers

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'));
like image 174
Ohgodwhy Avatar answered Jan 16 '23 21:01

Ohgodwhy