PHP Exception Throwing Eval
Wednesday, June 13th, 2007
function eeval($string)
{
$result = @eval($string);
if( isset($php_errormsg) )
throw new Exception($php_errormsg);
return $result;
}
function eeval($string)
{
$result = @eval($string);
if( isset($php_errormsg) )
throw new Exception($php_errormsg);
return $result;
}
In the process of writing a MogileFS client in PHP, I discovered there’s no no straight forward way to issue a PUT request using CURL where the body of the request comes from a string, rather than a file. Luckily, PHP 5.2 introduces memory based streams…
$fh = fopen('php://memory', 'rw');
fwrite($fh, $dataToPut);
rewind($fh);
$ch = curl_init();
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $length);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, 4);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Expect: '));
$response = curl_exec($ch);
fclose($fh);
PHP offers a way to parse a stream handle, but not way to parse just a string. Here’s a simple/lazy way to parse a single CSV line in PHP:
function parseCSV($str, $delimiter = ',', $enclosure = '"',
$len = 4096)
{
$fh = fopen('php://memory', 'rw');
fwrite($fh, $str);
rewind($fh);
$result = fgetcsv( $fh, $len, $delimiter, $enclosure );
fclose($fh);
return $result;
}