Hack for php < 5.5 :
function curl_reset(&$ch){
$ch = curl_init();
}
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
curl_reset — Reinicia todas las opciones de un manejador de sesión libcurl
Esta función reinicia todas las opciones definidas en el manejador cURL dado a sus valores por omisión.
ch
El recurso cURL devuelto por curl_init().
No devuelve ningún valor.
Versión | Descripción |
---|---|
8.0.0 |
handle expects a CurlHandle
instance now; previously, a resource was expected.
|
Ejemplo #1 Ejemplo con curl_reset()
<?php
// Crea un manejador cURL
$ch = curl_init();
// Define la opción CURLOPT_USERAGENT
curl_setopt($ch, CURLOPT_USERAGENT, "Mi user-agent de prueba");
// Reinicia todas las opciones definidas previamente
curl_reset($ch);
// Envía la petición HTTP
curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
curl_exec($ch); // el user-agent definido previamente no será enviado, fue reiniciado por la función curl_reset
// Cierra el manejador
curl_close($ch);
?>
Nota:
La función curl_reset() también reiniciará la URL proporcionada como argumento de la función curl_init().
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';
//...
$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. =^_^=