1--TEST-- 2cURL option CURLOPT_READFUNCTION 3--CREDITS-- 4WHITE new media architects - Jeroen Vermeulen 5#testfest Utrecht 2009 6--EXTENSIONS-- 7curl 8--FILE-- 9<?php 10function custom_readfunction($oCurl, $hReadHandle, $iMaxOut) 11{ 12 $sData = fread($hReadHandle,$iMaxOut-10); # -10 to have space to add "custom:" 13 if (!empty($sData)) 14 { 15 $sData = "custom:".$sData; 16 } 17 return $sData; 18} 19 20$sFileBase = __DIR__.DIRECTORY_SEPARATOR.'curl_opt_CURLOPT_READFUNCTION'; 21$sReadFile = $sFileBase.'_in.tmp'; 22$sWriteFile = $sFileBase.'_out.tmp'; 23$sWriteUrl = 'file://'.$sWriteFile; 24 25file_put_contents($sReadFile,'contents of tempfile'); 26$hReadHandle = fopen($sReadFile, 'r'); 27 28$oCurl = curl_init(); 29curl_setopt($oCurl, CURLOPT_URL, $sWriteUrl); 30curl_setopt($oCurl, CURLOPT_UPLOAD, 1); 31curl_setopt($oCurl, CURLOPT_READFUNCTION, "custom_readfunction" ); 32curl_setopt($oCurl, CURLOPT_INFILE, $hReadHandle ); 33curl_exec($oCurl); 34curl_close($oCurl); 35 36fclose ($hReadHandle); 37 38$sOutput = file_get_contents($sWriteFile); 39var_dump($sOutput); 40?> 41--CLEAN-- 42<?php 43$sFileBase = __DIR__.DIRECTORY_SEPARATOR.'curl_opt_CURLOPT_READFUNCTION'; 44$sReadFile = $sFileBase.'_in.tmp'; 45$sWriteFile = $sFileBase.'_out.tmp'; 46unlink($sReadFile); 47unlink($sWriteFile); 48?> 49--EXPECT-- 50string(27) "custom:contents of tempfile" 51