Builder.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php namespace Ixudra\Curl;
  2. class Builder {
  3. protected $curlObject = null;
  4. protected $curlOptions = array(
  5. 'RETURN_TRANSFER' => true,
  6. 'FAIL_ON_ERROR' => true,
  7. 'FOLLOW_LOCATION' => false,
  8. 'CONNECT_TIMEOUT' => '',
  9. 'TIMEOUT' => 30,
  10. 'USER_AGENT' => '',
  11. 'URL' => '',
  12. 'POST' => false,
  13. 'HTTP_HEADER' => array(),
  14. );
  15. protected $packageOptions = array(
  16. 'data' => array(),
  17. 'asJson' => false,
  18. );
  19. /**
  20. * Set the URL to which the request is to be sent
  21. * @param $url string The URL to which the request is to be sent
  22. * @return $this
  23. */
  24. public function to($url)
  25. {
  26. return $this->withCurlOption( 'URL', $url );
  27. }
  28. /**
  29. * Set the request timeout
  30. * @param $timeout integer The timeout for the request (in seconds. Default: 30 seconds)
  31. * @return $this
  32. */
  33. public function withTimeout($timeout = 30)
  34. {
  35. return $this->withCurlOption( 'TIMEOUT', $timeout );
  36. }
  37. /**
  38. * Add GET or POST data to the request
  39. * @param $data array Array of data that is to be sent along wiht the request
  40. * @return $this
  41. */
  42. public function withData($data = array())
  43. {
  44. return $this->withPackageOption( 'data', $data );
  45. }
  46. /**
  47. * Configure the package to encode and decode the request data
  48. * @return $this
  49. */
  50. public function asJson()
  51. {
  52. return $this->withPackageOption( 'asJson', true );
  53. }
  54. // /**
  55. // * Send the request over a secure connection
  56. // * @return $this
  57. // */
  58. // public function secure()
  59. // {
  60. // return $this;
  61. // }
  62. /**
  63. * Set any specific cURL option
  64. * @param $key string The name of the cURL option
  65. * @param $value string The value to which the option is to be set
  66. * @return $this
  67. */
  68. public function withOption($key, $value)
  69. {
  70. return $this->withCurlOption( $key, $value );
  71. }
  72. /**
  73. * Set any specific cURL option
  74. * @param $key string The name of the cURL option
  75. * @param $value string The value to which the option is to be set
  76. * @return $this
  77. */
  78. protected function withCurlOption($key, $value)
  79. {
  80. $this->curlOptions[ $key ] = $value;
  81. return $this;
  82. }
  83. /**
  84. * Set any specific package option
  85. * @param $key string The name of the cURL option
  86. * @param $value string The value to which the option is to be set
  87. * @return $this
  88. */
  89. protected function withPackageOption($key, $value)
  90. {
  91. $this->packageOptions[ $key ] = $value;
  92. return $this;
  93. }
  94. /**
  95. * Add a HTTP header to the request
  96. * @param $header string The HTTP header that is to be added to the request
  97. * @return $this
  98. */
  99. public function withHeader($header)
  100. {
  101. $this->curlOptions[ 'HTTP_HEADER' ][] = $header;
  102. }
  103. /**
  104. * Send a GET request to a URL using the specified cURL options
  105. * @return mixed
  106. */
  107. public function get()
  108. {
  109. $parameterString = '';
  110. if( is_array($this->packageOptions[ 'data' ]) && count($this->packageOptions[ 'data' ]) != 0 ) {
  111. $parameterString = '?'. http_build_query($this->packageOptions[ 'data' ]);
  112. }
  113. $this->curlOptions[ 'URL' ] .= $parameterString;
  114. return $this->send();
  115. }
  116. /**
  117. * Send a POST request to a URL using the specified cURL options
  118. * @return mixed
  119. */
  120. public function post()
  121. {
  122. $this->setPostParameters();
  123. return $this->send();
  124. }
  125. /**
  126. * Add POST parameters to the curlOptions array
  127. */
  128. protected function setPostParameters()
  129. {
  130. $this->curlOptions[ 'POST' ] = true;
  131. $parameters = $this->packageOptions[ 'data' ];
  132. if( $this->packageOptions[ 'asJson' ] ) {
  133. $parameters = json_encode($parameters);
  134. }
  135. $this->curlOptions[ 'POST_FIELDS' ] = $parameters;
  136. }
  137. // /**
  138. // * Send a PUT request to a URL using the specified cURL options
  139. // * @return mixed
  140. // */
  141. // public function put()
  142. // {
  143. // $this->setPostParameters();
  144. //
  145. // return $this->send();
  146. // }
  147. //
  148. // /**
  149. // * Send a DELETE request to a URL using the specified cURL options
  150. // * @return mixed
  151. // */
  152. // public function delete()
  153. // {
  154. // $this->setPostParameters();
  155. //
  156. // return $this->send();
  157. // }
  158. /**
  159. * Send the request
  160. * @return mixed
  161. */
  162. protected function send()
  163. {
  164. // Add JSON header if necessary
  165. if( $this->packageOptions[ 'asJson' ] ) {
  166. $this->withHeader( 'Content-Type: application/json' );
  167. }
  168. // Create the request with all specified options
  169. $this->curlObject = curl_init();
  170. $options = $this->forgeOptions();
  171. curl_setopt_array( $this->curlObject, $options );
  172. // Send the request
  173. $response = curl_exec( $this->curlObject );
  174. curl_close( $this->curlObject );
  175. // Decode the request if necessary
  176. if( $this->packageOptions[ 'asJson' ] ) {
  177. $response = json_decode($response);
  178. }
  179. // Return the result
  180. return $response;
  181. }
  182. /**
  183. * Convert the curlOptions to an array of usable options for the cURL request
  184. * @return array
  185. */
  186. protected function forgeOptions()
  187. {
  188. $results = array();
  189. foreach( $this->curlOptions as $key => $value ) {
  190. $array_key = constant( 'CURLOPT_' . $key );
  191. if( $key == 'POST_FIELDS' && is_array( $value ) ) {
  192. $results[ $array_key ] = http_build_query( $value, null, '&' );
  193. } else {
  194. $results[ $array_key ] = $value;
  195. }
  196. }
  197. return $results;
  198. }
  199. }