note 120224 added to function.curl-reset
| From: | dev at codesatori dot com | Date: | Fri, 25 Nov 2016 11:44:09 +0000 |
| Subject: | note 120224 added to function.curl-reset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207693@lists.php.net to get a copy of this message | ||
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. =^_^=
----
Server IP: 72.52.91.14
Probable Submitter: 128.199.128.196
----
Manual Page -- http://php.net/manual/en/function.curl-reset.php
Edit -- https://master.php.net/note/edit/120224
Del: integrated -- https://master.php.net/note/delete/120224/integrated
Del: useless -- https://master.php.net/note/delete/120224/useless
Del: bad code -- https://master.php.net/note/delete/120224/bad+code
Del: spam -- https://master.php.net/note/delete/120224/spam
Del: non-english -- https://master.php.net/note/delete/120224/non-english
Del: in docs -- https://master.php.net/note/delete/120224/in+docs
Del: other reasons-- https://master.php.net/note/delete/120224
Reject -- https://master.php.net/note/reject/120224
Search -- https://master.php.net/manage/user-notes.php