How to get an option previously set with curl_setopt()?
I'm just wondering as there is no curl_getopt() function, how it is possible to find out which value has been set for a specific option with cur开发者_运维问答l_setopt()
previously?
Pulled from various answers around the internets:
Question: Is there a way to get the current curl option settings? Like a curl_getopt() or curl_showopts()?
Answer: Yes and no. There is curl_getinfo() which will show you some info about the last connection, but I suspect it's not what you're looking for. It's a weakness in curl, IMHO.
My suggestion (and others) is to encapsulate cURL into a class where your $cURL->setOpt()
function also stores the value for retrieval later.
The multirequest PHP library has this functionality (and then some!):
$request = new \MultiRequest\Request($url);
$request->setCurlOption(CURLOPT_PROXY, $proxy);
// ...
$curlOptions = $request->getCurlOptions();
list($proxyIp, $proxyPort) = explode(':', $curlOptions[CURLOPT_PROXY]);
Possibly curl_getinfo()
may satisfy some of your needs.
If not, you can write a wrapper of curl_setopt()
which saves all options to an array.
// in C version of libcurl
CURLcode code;
CURL *curl = curl_easy_init();
if (curl){
// Set CURL option
code = curl_easy_setopt(curl,
CURLOPT_URL,
"https://curl.se/libcurl/c/CURLINFO_EFFECTIVE_URL.html");
// Get CURL option
char *request_url = NULL;
code = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &request_url);
if (CURLE_OK == code && request_url){
printf("URL: %s", request_url);
}
curl_easy_cleanup(curl);
}
精彩评论