Builder.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php namespace Ixudra\Curl;
  2. class Builder {
  3. /** @var resource $curlObject cURL request */
  4. protected $curlObject = null;
  5. /** @var array $curlOptions Array of cURL options */
  6. protected $curlOptions = array(
  7. 'RETURNTRANSFER' => true,
  8. 'FAILONERROR' => true,
  9. 'FOLLOWLOCATION' => false,
  10. 'CONNECTTIMEOUT' => '',
  11. 'TIMEOUT' => 30,
  12. 'USERAGENT' => '',
  13. 'URL' => '',
  14. 'POST' => false,
  15. 'HTTPHEADER' => array(),
  16. );
  17. /** @var array $packageOptions Array with options that are not specific to cURL but are used by the package */
  18. protected $packageOptions = array(
  19. 'data' => array(),
  20. 'asJson' => false,
  21. 'returnAsArray' => false,
  22. 'saveFile' => '',
  23. );
  24. /**
  25. * Set the URL to which the request is to be sent
  26. *
  27. * @param $url string The URL to which the request is to be sent
  28. * @return Builder
  29. */
  30. public function to($url)
  31. {
  32. return $this->withCurlOption( 'URL', $url );
  33. }
  34. /**
  35. * Set the request timeout
  36. *
  37. * @param integer $timeout The timeout for the request (in seconds. Default: 30 seconds)
  38. * @return Builder
  39. */
  40. public function withTimeout($timeout = 30)
  41. {
  42. return $this->withCurlOption( 'TIMEOUT', $timeout );
  43. }
  44. /**
  45. * Add GET or POST data to the request
  46. *
  47. * @param array $data Array of data that is to be sent along with the request
  48. * @return Builder
  49. */
  50. public function withData($data = array())
  51. {
  52. return $this->withPackageOption( 'data', $data );
  53. }
  54. /**
  55. * Configure the package to encode and decode the request data
  56. *
  57. * @param boolean $asArray Indicates whether or not the data should be returned as an array. Default: false
  58. * @return Builder
  59. */
  60. public function asJson($asArray = false)
  61. {
  62. return $this->withPackageOption( 'asJson', true )
  63. ->withPackageOption( 'returnAsArray', $asArray );
  64. }
  65. // /**
  66. // * Send the request over a secure connection
  67. // *
  68. // * @return Builder
  69. // */
  70. // public function secure()
  71. // {
  72. // return $this;
  73. // }
  74. /**
  75. * Set any specific cURL option
  76. *
  77. * @param string $key The name of the cURL option
  78. * @param string $value The value to which the option is to be set
  79. * @return Builder
  80. */
  81. public function withOption($key, $value)
  82. {
  83. return $this->withCurlOption( $key, $value );
  84. }
  85. /**
  86. * Set any specific cURL option
  87. *
  88. * @param string $key The name of the cURL option
  89. * @param string $value The value to which the option is to be set
  90. * @return Builder
  91. */
  92. protected function withCurlOption($key, $value)
  93. {
  94. $this->curlOptions[ $key ] = $value;
  95. return $this;
  96. }
  97. /**
  98. * Set any specific package option
  99. *
  100. * @param string $key The name of the cURL option
  101. * @param string $value The value to which the option is to be set
  102. * @return Builder
  103. */
  104. protected function withPackageOption($key, $value)
  105. {
  106. $this->packageOptions[ $key ] = $value;
  107. return $this;
  108. }
  109. /**
  110. * Add a HTTP header to the request
  111. *
  112. * @param string $header The HTTP header that is to be added to the request
  113. * @return Builder
  114. */
  115. public function withHeader($header)
  116. {
  117. $this->curlOptions[ 'HTTPHEADER' ][] = $header;
  118. return $this;
  119. }
  120. /**
  121. * Add a content type HTTP header to the request
  122. *
  123. * @param string $contentType The content type of the file you would like to download
  124. * @return Builder
  125. */
  126. public function withContentType($contentType)
  127. {
  128. return $this->withHeader( 'Content-Type: '. $contentType )
  129. ->withHeader( 'Connection: Keep-Alive' );
  130. }
  131. /**
  132. * Send a GET request to a URL using the specified cURL options
  133. *
  134. * @return mixed
  135. */
  136. public function get()
  137. {
  138. $parameterString = '';
  139. if( is_array($this->packageOptions[ 'data' ]) && count($this->packageOptions[ 'data' ]) != 0 ) {
  140. $parameterString = '?'. http_build_query($this->packageOptions[ 'data' ]);
  141. }
  142. $this->curlOptions[ 'URL' ] .= $parameterString;
  143. return $this->send();
  144. }
  145. /**
  146. * Send a POST request to a URL using the specified cURL options
  147. *
  148. * @return mixed
  149. */
  150. public function post()
  151. {
  152. $this->setPostParameters();
  153. return $this->send();
  154. }
  155. /**
  156. * Send a download request to a URL using the specified cURL options
  157. *
  158. * @param string $fileName
  159. * @return mixed
  160. */
  161. public function download($fileName)
  162. {
  163. $this->packageOptions[ 'saveFile' ] = $fileName;
  164. return $this->send();
  165. }
  166. /**
  167. * Add POST parameters to the curlOptions array
  168. */
  169. protected function setPostParameters()
  170. {
  171. $this->curlOptions[ 'POST' ] = true;
  172. $parameters = $this->packageOptions[ 'data' ];
  173. if( $this->packageOptions[ 'asJson' ] ) {
  174. $parameters = json_encode($parameters);
  175. }
  176. $this->curlOptions[ 'POSTFIELDS' ] = $parameters;
  177. }
  178. // /**
  179. // * Send a PUT request to a URL using the specified cURL options
  180. // *
  181. // * @return mixed
  182. // */
  183. // public function put()
  184. // {
  185. // $this->setPostParameters();
  186. //
  187. // return $this->send();
  188. // }
  189. //
  190. // /**
  191. // * Send a DELETE request to a URL using the specified cURL options
  192. // *
  193. // * @return mixed
  194. // */
  195. // public function delete()
  196. // {
  197. // $this->setPostParameters();
  198. //
  199. // return $this->send();
  200. // }
  201. /**
  202. * Send the request
  203. *
  204. * @return mixed
  205. */
  206. protected function send()
  207. {
  208. // Add JSON header if necessary
  209. if( $this->packageOptions[ 'asJson' ] ) {
  210. $this->withHeader( 'Content-Type: application/json' );
  211. }
  212. // Create the request with all specified options
  213. $this->curlObject = curl_init();
  214. $options = $this->forgeOptions();
  215. curl_setopt_array( $this->curlObject, $options );
  216. // Send the request
  217. $response = curl_exec( $this->curlObject );
  218. curl_close( $this->curlObject );
  219. if( $this->packageOptions[ 'saveFile' ] ) {
  220. // Save to file if a filename was specified
  221. $file = fopen($this->packageOptions[ 'saveFile' ], 'w');
  222. fwrite($file, $response);
  223. fclose($file);
  224. } else if( $this->packageOptions[ 'asJson' ] ) {
  225. // Decode the request if necessary
  226. $response = json_decode($response, $this->packageOptions[ 'returnAsArray' ]);
  227. }
  228. // Return the result
  229. return $response;
  230. }
  231. /**
  232. * Convert the curlOptions to an array of usable options for the cURL request
  233. *
  234. * @return array
  235. */
  236. protected function forgeOptions()
  237. {
  238. $results = array();
  239. foreach( $this->curlOptions as $key => $value ) {
  240. $array_key = constant( 'CURLOPT_' . $key );
  241. if( $key == 'POSTFIELDS' && is_array( $value ) ) {
  242. $results[ $array_key ] = http_build_query( $value, null, '&' );
  243. } else {
  244. $results[ $array_key ] = $value;
  245. }
  246. }
  247. return $results;
  248. }
  249. }