my life

day to day

Archive for June 13th, 2007

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;
  }

add to del.icio.us    add to technorati favs   email this

PHP CURL PUT String

Wednesday, June 13th, 2007

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($chCURLOPT_INFILE$fh);
    curl_setopt($chCURLOPT_INFILESIZE$length);
    curl_setopt($chCURLOPT_TIMEOUT10);
    curl_setopt($chCURLOPT_PUT4);
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_HTTPHEADER, Array('Expect: '));
    $response curl_exec($ch);
    fclose($fh);

add to del.icio.us    add to technorati favs   email this

PHP CSV Parsing

Wednesday, June 13th, 2007

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;
}

add to del.icio.us    add to technorati favs   email this