curl_reset

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

curl_resetRedefine todas as opções de um identificador de sessão libcurl

Descrição

curl_reset(CurlHandle $handle): void

Essa função reinicializa todas as opções configuradas no identificador cURL informado para os valores padrões.

Parâmetros

handle

Um manipulador cURL retornado por curl_init().

Valor Retornado

Nenhum valor é retornado.

Registro de Alterações

Versão Descrição
8.0.0 O parâmetro handle agora espera uma instância de CurlHandle; anteriormente, um resource era esperado.

Exemplos

Exemplo #1 Exemplo de curl_reset()

<?php
// Cria um identificador curl
$ch = curl_init();

// Configura a opção CURLOPT_USERAGENT
curl_setopt($ch, CURLOPT_USERAGENT, "My test user-agent");

// Redefine todas as opções que foram configuradas anteriormente
curl_reset($ch);

// Envia a requisição HTTP
curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
curl_exec($ch); // o user-agent configurado anteriormente não será enviado porque foi redefinido por curl_reset

// Fecha o identificador
curl_close($ch);
?>

Notas

Nota:

curl_reset() também reseta a URL informada no parâmetro curl_init().

Veja Também

  • curl_setopt() - Define uma opção para uma transferência cURL

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
10
Waloon
9 years ago
Hack for php < 5.5 : function curl_reset(&$ch){  $ch = curl_init();}
up
1
dev at codesatori dot com
8 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:<?phpclass 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';        //...        $result = $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. =^_^=
To Top