PHP 8.6.0 Beta 1 is available for testing

curl_reset

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

curl_resetСбрасывает опции дескриптора сессии библиотеки libcurl

Описание

function curl_reset(CurlHandle $handle): void

Функция переинициализирует опции заданного обработчика cURL-сессии значениями по умолчанию.

Список параметров

handle
Дескриптор модуля cURL, который вернула функция curl_init() .

Возвращаемые значения

Функция не возвращает значения после выполнения.

Список изменений

Версия Описание
8.0.0 Параметр handle теперь принимает объект CurlHandle ; раньше, параметр ждал ресурс (resource ).

Примеры

Пример #1 Пример использования функции curl_reset()

<?php
// Создаём cURL-дескриптор
$ch = curl_init();
// Устанавливаем опцию CURLOPT_USERAGENT
curl_setopt($ch, CURLOPT_USERAGENT, "My test user-agent");
// Сбрасываем установленные опции
curl_reset($ch);
// Посылаем дескриптор-запрос
curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
curl_exec($ch); // Функция не отправит пользовательский агент, который установили раньше,
 // агент сбросили функцией curl_reset
?>

Примечания

Замечание:

Функция curl_reset() также сбрасывает URL-адрес который указали в параметре функции curl_init() .

Смотрите также

  • curl_setopt() - Устанавливает параметр для cURL-передачи

Нашли ошибку?

ИнструкцияИсправлениеСообщение об ошибке
+Добавить

Примечания пользователей 2 notes

up
8
Waloon
10 years ago
Hack for php < 5.5 : 
function curl_reset(&$ch){
 $ch = curl_init();
}
up
1
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';
 //...
 $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. =^_^=
+Добавить

AltStyle によって変換されたページ (->オリジナル) /