CURL.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. *
  4. * Description
  5. *
  6. * @package Paystack
  7. * @category Source
  8. * @author Michael Akanji <matscode@gmail.com>
  9. * @date 2017-06-25
  10. * @copyright (c) 2016 - 2017, TECRUM (http://www.tecrum.com)
  11. *
  12. */
  13. namespace Matscode\Paystack;
  14. class CURL
  15. {
  16. private
  17. $_url,
  18. $_curl,
  19. $_requestWithPost = false,
  20. $_allowedReqMethodForPost =
  21. [
  22. 'POST',
  23. 'UPDATE',
  24. 'PUT',
  25. 'PATCH',
  26. ],
  27. $_errorCode,
  28. $_errorMessage;
  29. public
  30. $requestMethod,
  31. $requestHeader = [];
  32. const
  33. USER_AGENT = 'PHP CURL/1.0 (@matscode)';
  34. public function __construct( $url, $requestMethod = null )
  35. {
  36. if ( ! extension_loaded( 'curl' ) ) {
  37. throw new \ErrorException( 'CURL Extension not loaded, Install libcurl php extension' );
  38. }
  39. //initialize CUrl
  40. $this->_url = $url;
  41. $this->_curlInit( $this->_url );
  42. //set requestMethod property
  43. $this->requestMethod = $requestMethod;
  44. //set the default request method to 'POST'
  45. if ( ! is_null( $this->requestMethod ) ) {
  46. $this->setRequestMethod( $this->requestMethod );
  47. }
  48. return $this;
  49. }
  50. private function _curlInit( $url )
  51. {
  52. $this->_curl = curl_init();
  53. $this->setOption( CURLOPT_USERAGENT, self::USER_AGENT )
  54. ->setOption( CURLOPT_URL, $url )
  55. // return response
  56. ->setOption( CURLOPT_RETURNTRANSFER, true );
  57. // turn off caching
  58. $this->setRequestHeader( 'Cache-Control', 'no-cache' );
  59. return $this;
  60. }
  61. public function setOption( $option, $value )
  62. {
  63. // Set curl option
  64. curl_setopt( $this->_curl, $option, $value );
  65. return $this;
  66. }
  67. public function setRequestMethod( $requestMethod )
  68. {
  69. switch ( $requestMethod ) {
  70. case 'GET' :
  71. $this->setOption( CURLOPT_HTTPGET, true );
  72. break;
  73. case 'UPDATE' :
  74. $this->setOption( CURLOPT_CUSTOMREQUEST, 'UPDATE' );
  75. break;
  76. case 'PUT' :
  77. $this->setOption( CURLOPT_CUSTOMREQUEST, 'PUT' );
  78. break;
  79. case 'PATCH' :
  80. $this->setOption( CURLOPT_CUSTOMREQUEST, 'PATCH' );
  81. break;
  82. case 'DELETE' :
  83. $this->setOption( CURLOPT_CUSTOMREQUEST, 'DELETE' );
  84. break;
  85. case 'POST':
  86. $this->doPostRequest();
  87. default:
  88. $this->setOption( CURLOPT_POST, true );
  89. break;
  90. }
  91. return $this;
  92. }
  93. /**
  94. * @param null $qStringArray
  95. *
  96. * @return string
  97. */
  98. public function getUrl( $qStringArray = null )
  99. {
  100. if ( ! is_null( $qStringArray ) &&
  101. ( is_array( $qStringArray ) || is_object( $qStringArray ) )
  102. ) {
  103. $this->_url .= '?' . http_build_query( $qStringArray );
  104. }
  105. return $this->_url;
  106. }
  107. /**
  108. * @param string $url
  109. *
  110. * @return $this
  111. */
  112. public function setUrl(
  113. $url
  114. ) {
  115. $this->setOption( CURLOPT_URL, $url );
  116. $this->_url = $url;
  117. return $this;
  118. }
  119. public function doPostRequest()
  120. {
  121. if ( in_array( $this->requestMethod, $this->_allowedReqMethodForPost ) ) {
  122. $this->_requestWithPost = true;
  123. }
  124. return $this;
  125. }
  126. public function run( $data, $as = 'urlencoded', $closeCurl = false ) // urlencoded | json | form-data
  127. {
  128. if ( $this->_requestWithPost ) {
  129. //make a post request
  130. switch ( $as ) {
  131. case 'json':
  132. if ( is_array( $data ) ) {
  133. $data = json_encode( $data );
  134. $this->setRequestHeader( 'Content-Type', 'application/json' );
  135. } else {
  136. throw new \ErrorException( 'Data argument passed to the run method must be of datatype Array when posting as JSON' );
  137. }
  138. break;
  139. case 'form-data':
  140. if ( is_array( $data ) || is_object( $data ) ) {
  141. $this->setRequestHeader( 'Content-Type', 'multipart/form-data' );
  142. } else {
  143. throw new \ErrorException( 'Data argument passed to the run method must be of datatype Array or Object when postiing as FORM-DATA' );
  144. }
  145. break;
  146. case 'urlenconded':
  147. // convert data to queryString - Native
  148. $data = http_build_query( $data );
  149. // continue to default
  150. default:
  151. $this->setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
  152. break;
  153. }
  154. $this->setOption( CURLOPT_POSTFIELDS, $data ); //Post Fields
  155. } else {
  156. //make a built query string and reset CURLOPT_URL
  157. $this->setOption( CURLOPT_URL, $this->getUrl( $data ) );
  158. }
  159. // execute curl
  160. $response = curl_exec( $this->_curl );
  161. // save error details in memory
  162. $this->_errorCode = curl_errno( $this->_curl );
  163. $this->_errorMessage = curl_error( $this->_curl );
  164. if ( $closeCurl ) {
  165. // close curl connection by default
  166. if ( is_resource( $this->_curl ) ) {
  167. curl_close( $this->_curl );
  168. }
  169. }
  170. // return response from endpoint
  171. return $response;
  172. }
  173. /**
  174. * @param $key
  175. * @param $value
  176. *
  177. * @return $this
  178. */
  179. public function setRequestHeader( $key, $value = null )
  180. {
  181. if ( ! is_array( $key ) ) {
  182. // assist with capitalizing http header keys
  183. $headers[] = ucwords( $key ) . ': ' . $value;
  184. } else {
  185. $headers = $key;
  186. }
  187. // merge requestHeader to base header
  188. $this->requestHeader = array_merge( $headers, $this->requestHeader );
  189. $this->setOption( CURLOPT_HTTPHEADER, $this->requestHeader );
  190. return $this;
  191. }
  192. public function getErrorCode()
  193. {
  194. return $this->_errorCode;
  195. }
  196. public function getErrorMessage()
  197. {
  198. return $this->_errorMessage;
  199. }
  200. /**
  201. * @return array
  202. */
  203. public function getRequestHeader()
  204. {
  205. return $this->requestHeader;
  206. }
  207. }