略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: curl_reset

2024-11-15

curl_reset

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

curl_reset重置一个 libcurl 会话句柄的所有的选项

说明

curl_reset(resource $ch): void

该函数将给定的 cURL 句柄所有选项重新设置为默认值。

参数

handle

curl_init() 返回的 cURL 句柄。

返回值

没有返回值。

范例

示例 #1 curl_reset() 示例

<?php
// 创建一个url 句柄
$ch curl_init();

// 设置 CURLOPT_USERAGENT 选项
curl_setopt($chCURLOPT_USERAGENT"My test user-agent");

// 重置所有的预先设置的选项
curl_reset($ch);

// 发送 HTTP 请求
curl_setopt($chCURLOPT_URL'http://example.com/');
curl_exec($ch); // 预先设置的 user-agent 不会被发送,它已经被 curl_reset 重置掉了

// 关闭句柄
curl_close($ch);
?>

注释

注意:

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

参见

add a noteadd a note

User Contributed Notes 2 notes

up
11
Waloon
5 years ago
Hack for php < 5.5 :

function curl_reset(&$ch){
  $ch = curl_init();
}
up
1
dev at codesatori dot com
5 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. =^_^=

官方地址:https://www.php.net/manual/en/function.curl-reset.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3