开发者

PHP Curl, where can I find a list of integer equivelants of constants

I'm using the Codeigniter Curl library, and the author uses the integer e开发者_开发知识库quivalents of the curl options. The php manual says this for curl_setopt_array:

The keys should be valid curl_setopt() constants or their integer equivalents.

How do I figure out what the integer equivalents are for a constant? I've googled it but haven't found anything.

Thanks, Mark


$arr = get_defined_constants(true);
var_dump($arr['curl']);


To expand on ajreal's answer

$constants = get_defined_constants(true);
$curlOptLookup = preg_grep('/^CURLOPT_/', array_flip($constants['curl']));

var_dump($curlOptLookup);

The above gives an integer lookup, so the following would work:

echo $curlOptLookup[119]; // outputs "CURLOPT_FTP_SSL"

If you want the options, the correct way round it needs to be flipped again:

$curlOpts = array_flip($curlOptLookup);

echo $curlOpts['CURLOPT_FTP_SSL']; // outputs 119


@Orbling 's technique for getting an array indexed by the curlopt values is mostly correct, except you should filter out the CURLOPT_* elements before flipping the array, since there are collisions in values between the various CURLOPT_*, CURLE_*, CURLSSH_* (etc.) options that are not simply aliases for each other.

For example, CURLOPT_POST and CURLE_TOO_MANY_REDIRECTS both have the value 47. When you call array_flip($constants['curl']), the latter entry overwrites the former, and you lose CURLOPT_POST.

So maybe do this instead:

$constants = get_defined_constants(true);
$curlOptKeys = preg_grep('/^CURLOPT_/', array_keys($constants['curl']));
$curlOpts = array_intersect_key($constants['curl'], array_flip($curlOptKeys));
$curlOptLookup = array_flip($curlOpts);

echo $curlOpts['CURLOPT_POST']; // 47
echo $curlOptLookup[47]; // 'CURLOPT_POST'


Echo/print them...

Example:

<?php
echo(CURLOPT_URL);


Just beware that some constants have the same value associated with them, in the answers above these will get overwritten. To get a full list you can not populate an array.

$arr = get_defined_constants(true);
foreach ($arr['curl'] as $k => $v) {
    if (preg_match('/CURLOPT/',$k)) {
            print $v." => '$k',\n";
    }
}

For example Output For 10026:-

10026 => 'CURLOPT_SSLCERTPASSWD'
10026 => 'CURLOPT_SSLKEYPASSWD'
10026 => 'CURLOPT_KEYPASSWD'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜