user969068
user969068

Reputation: 2943

curl callback in codeigniter

I have a callback function for CURLOPT_PROGRESSFUNCTION in codeigniter like below:

class download_model extends CI_Model {
function downloadIt($link){

  $targetFile = FCPATH."upload/".basename($link);
  $handle = fopen($targetFile, 'w');

   $ch = curl_init( $link );
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
   curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
   curl_setopt( $ch, CURLOPT_FILE, $handle );
   curl_setopt( $ch, CURLOPT_BUFFERSIZE, 2 );
   curl_exec( $ch );
   curl_close($ch);
}

function progressCallback( $params )
{
// params here
}

}

but i get below error using above code:

A PHP Error was encountered

Severity: Warning

Message: curl_exec() [function.curl-exec]: Cannot call the CURLOPT_PROGRESSFUNCTION

And if i move that function progressCallback() to root index.php it works properly, what would be efficient way to add this function in codeigniter so curl callback will recognize it? Regards

Upvotes: 0

Views: 1133

Answers (1)

Rooneyl
Rooneyl

Reputation: 7902

How to add callback function for native PHP functions in Codeigniter.

curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, array('name_of_controller', 'name_of_function' );

This is how I do it when I use functions like usort();

Upvotes: 3

Related Questions