Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP cURL: Reset Callback (CURLOPT_HEADERFUNCTION)

I have a code similar to the following:

$ch = curl_init();
// ...
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header_line) {
    // process $header_line...
    return strlen($header_line); // needed by curl
});
// ...
curl_exec($ch);

After that code I’d like to keep using $ch to re-use the connection, but I don’t need the custom CURLOPT_HEADERFUNCTION callback anymore. How can I reset it? I tried setting it to null but it didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Set the common options in an array and reset options between executions:

$common_options = [
    CURLOPT_XXX => 'something'
    // ...
];
$ch = curl_init();

// First call: common + specific options
curl_setopt_array($ch, $common_options);
curl_setopt($ch, CURLOPT_FOO, 'something else');

curl_exec($ch);

// Second call: common options only
curl_reset($ch);
curl_setopt_array($ch, $common_options);

curl_exec($ch);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading