curl_object.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * CurlInterface
  4. *
  5. * An object-oriented shim that wraps the standard PHP cURL library.
  6. *
  7. * This interface has been created so that cURL functionality can be stubbed
  8. * out for unit testing, or swapped for an alternative library.
  9. *
  10. * @see curl
  11. * @package
  12. * @version $id$
  13. */
  14. interface CurlInterface {
  15. /**
  16. * errNo
  17. *
  18. * Encapsulates curl_errno - Returns the last error number
  19. * @param resource $ch - A cURL handle returned by init.
  20. * @access public
  21. * @return the error number or 0 if no error occured.
  22. */
  23. public function errno($ch);
  24. /**
  25. * error
  26. *
  27. * Encapsulates curl_error - Return last error string
  28. * @param resource $ch - A cURL handle returned by init.
  29. * @access public
  30. * @return the error messge or '' if no error occured.
  31. */
  32. public function error($ch);
  33. /**
  34. * execute
  35. *
  36. * Encapsulates curl_exec - Perform a cURL session.
  37. * @param resource $ch - A cURL handle returned by init.
  38. * @access public
  39. * @return TRUE on success, FALSE on failure.
  40. */
  41. public function execute($ch);
  42. /**
  43. * init
  44. *
  45. * Encapsulates curl_init - Initialize a cURL session.
  46. * @param string $url - url to use.
  47. * @access public
  48. * @return cURL handle on success, FALSE on failure.
  49. */
  50. public function init($url);
  51. /**
  52. * setopt
  53. *
  54. * Encapsulates curl_setopt - Set an option for cURL transfer.
  55. * @param resource $ch - A cURL handle returned by init.
  56. * @param int $opt - The CURLOPT to set.
  57. * @param mixed $value - The value to set.
  58. * @access public
  59. * @return True on success, FALSE on failure.
  60. */
  61. public function setopt ($ch , $option , $value);
  62. }
  63. /**
  64. * CurlObject
  65. *
  66. * A concrete implementation of CurlInterface using the PHP cURL library.
  67. *
  68. * @package
  69. * @version $id$
  70. */
  71. class CurlObject implements CurlInterface {
  72. public function errno($ch) {
  73. return curl_errno($ch);
  74. }
  75. public function error($ch) {
  76. return curl_error($ch);
  77. }
  78. public function execute($ch) {
  79. return curl_exec($ch);
  80. }
  81. public function init($url) {
  82. return curl_init($url);
  83. }
  84. public function setopt ($ch , $option , $value) {
  85. return curl_setopt($ch, $option, $value);
  86. }
  87. }
  88. ?>