PHP: curl_reset - Manual (original) (raw)

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

curl_reset — Reset all options of a libcurl session handle

Description

Parameters

handle

A cURL handle returned bycurl_init().

Return Values

No value is returned.

Changelog

Version Description
8.0.0 handle expects a CurlHandle instance now; previously, a resource was expected.

Examples

Example #1 curl_reset() example

<?php // Create a curl handle $ch = curl_init();// Set CURLOPT_USERAGENT option curl_setopt($ch, CURLOPT_USERAGENT, "My test user-agent");// Reset all previously set options curl_reset($ch);// Send HTTP request curl_setopt($ch, CURLOPT_URL, 'http://example.com/'); curl_exec($ch); // the previously set user-agent will be not sent, it has been reset by curl_reset ?>

Notes

Note:

curl_reset() also resets the URL given as the curl_init() parameter.

Found A Problem?

Waloon

9 years ago

Hack for php < 5.5 : 

function curl_reset(&$ch){
  $ch = curl_init();
}

dev at codesatori dot com

9 years ago

If you're reusing a cUrl handle and want to ensure there's no residue from previous options -- but are frustrated with resetting the basics (e.g. FTP details) needed for each cURL call -- then here's an easy pattern to fix that:

<?php
class cUrlicue {
    
    protected $curl;

    /* Create the cURL handle */

    function __construct() {
        $this->curl = curl_init();
        $this->curl_init_opts();
        curl_exec($this->curl);
    }
    
    /* Reload your base options */

    function curl_init_opts() {
        $opts[CURLOPT_PROTOCOLS]         = CURLPROTO_FTP;
        $opts[CURLOPT_RETURNTRANSFER]     = true;
        $opts[CURLOPT_USERPWD]             = 'user:pass';
        //...
        curl_setopt_array($this->curl, $opts);
    }
    
    /* Use when making a new cURL call */

    function curl_exec($opts) {
        curl_reset($this->curl); // clears all old options
        $this->curl_init_opts(); // sets base options again
        curl_setopt_array($this->curl, $opts); // sets your new options
        return curl_exec($this->curl);
    }
    
    /* Your whatever cURL method */

    function curl_get_whatever() {
        $opts[CURLOPT_URL] = 'ftp://.../whatever';
        //...
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>this->curl_exec($opts);
        // ...
    }    
}
?>

Then: each call to $this->curl_exec() from your whatever-method resets the previous options, reloads the base options, adds in your new options, and returns the result. Otherwise, can also put your base options into a class property, instead of in-method, if there's nothing dynamic being defined. Enjoy. =^_^=