curl_setopt_CURLOPT_READFUNCTION.phpt 1.4 KB

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