3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hhb_curl_init($custom_options_array = array()) { if(empty($custom_options_array)){ $custom_options_array=array(); //i feel kinda bad about this.. argv[1] of curl_init wants a string(url), or NULL //at least i want to allow NULL aswell :/ } if (!is_array($custom_options_array)) { throw new InvalidArgumentException('$custom_options_array must be an array!'); }; $options_array = array( CURLOPT_AUTOREFERER => true, CURLOPT_BINARYTRANSFER => true, CURLOPT_COOKIESESSION => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_FORBID_REUSE => false, CURLOPT_HTTPGET => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 11, //CURLOPT_REFERER=>'hanshenrik.tk', ); if (!array_key_exists(CURLOPT_COOKIEFILE, $custom_options_array)) { //do this only conditionally because tmpfile() call.. //we want PHP5.3- support -.- //for 5.4, just: $options_array[CURLOPT_COOKIEFILE]=stream_get_meta_data(tmpfile())['uri']; //static arr workaround for https://bugs.php.net/bug.php?id=66014 ... static $curl_cookiefile_filehandle_arr=array(); $curl_filehandle_arr[]=$options_array[CURLOPT_COOKIEFILE] = tmpfile(); $options_array[CURLOPT_COOKIEFILE] =stream_get_meta_data($options_array[CURLOPT_COOKIEFILE]); $options_array[CURLOPT_COOKIEFILE]=$options_array[CURLOPT_COOKIEFILE]['uri']; } //we can't use array_merge() because of how it handles integer-keys, it would/could cause corruption foreach($custom_options_array as $key => $val) { $options_array[$key] = $val; } unset($key, $val, $custom_options_array); $curl = curl_init(); curl_setopt_array($curl, $options_array); return $curl; } for($i=0;$i<9;++$i){ var_dump(hhb_curl_init()); }

preferences:
41.86 ms | 402 KiB | 5 Q