Search code examples
phpapachecurldllserver

PHP cUrl get all integrated variables data


I want to loop through all available cUrl integrated commands.

I know that this is not the current syntax but just to clarify so you'll get the idea...

$s = curl_init();

foreach (curl_setopt($s, [THIS IS WHAT I AM LOOKING FOR] ) as $q) {
    echo $q . '<br />';
}

I'm expecting to get all integrated cUrl commands (for example):

CURLOPT_URL
CURLOPT_HTTPHEADER
CURLOPT_TIMEOUT
etc...

Solution

  • get_defined_constants return all defined constants

    These constants contain curl commands you want!

    php.net

    get all curl defined variables :

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

    and if you want just "CURLOPT_" options, use this:

    $arr = get_defined_constants(true);
    $curl_vars = $arr['curl'];
    
    $array = array_filter(array_keys($curl_vars), function ($k){ 
        return strpos($k, 'CURLOPT_') === 0;
    });
    

    or you can use foreach to do this:

    $arr = get_defined_constants(true);
    $curl_vars = $arr['curl'];
    
    foreach ($curl_vars as $key => $value) {
        if(strpos($key,'CURLOPT_') === 0){
            echo $key; // here!
        }
    }