123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php namespace Ixudra\Curl;
- class Builder {
- /** @var resource $curlObject cURL request */
- protected $curlObject = null;
- /** @var array $curlOptions Array of cURL options */
- protected $curlOptions = array(
- 'RETURNTRANSFER' => true,
- 'FAILONERROR' => true,
- 'FOLLOWLOCATION' => false,
- 'CONNECTTIMEOUT' => '',
- 'TIMEOUT' => 30,
- 'USERAGENT' => '',
- 'URL' => '',
- 'POST' => false,
- 'HTTPHEADER' => array(),
- );
- /** @var array $packageOptions Array with options that are not specific to cURL but are used by the package */
- protected $packageOptions = array(
- 'data' => array(),
- 'asJson' => false,
- 'returnAsArray' => false,
- 'saveFile' => '',
- );
- /**
- * Set the URL to which the request is to be sent
- *
- * @param $url string The URL to which the request is to be sent
- * @return Builder
- */
- public function to($url)
- {
- return $this->withCurlOption( 'URL', $url );
- }
- /**
- * Set the request timeout
- *
- * @param integer $timeout The timeout for the request (in seconds. Default: 30 seconds)
- * @return Builder
- */
- public function withTimeout($timeout = 30)
- {
- return $this->withCurlOption( 'TIMEOUT', $timeout );
- }
- /**
- * Add GET or POST data to the request
- *
- * @param array $data Array of data that is to be sent along with the request
- * @return Builder
- */
- public function withData($data = array())
- {
- return $this->withPackageOption( 'data', $data );
- }
- /**
- * Configure the package to encode and decode the request data
- *
- * @param boolean $asArray Indicates whether or not the data should be returned as an array. Default: false
- * @return Builder
- */
- public function asJson($asArray = false)
- {
- return $this->withPackageOption( 'asJson', true )
- ->withPackageOption( 'returnAsArray', $asArray );
- }
- // /**
- // * Send the request over a secure connection
- // *
- // * @return Builder
- // */
- // public function secure()
- // {
- // return $this;
- // }
- /**
- * Set any specific cURL option
- *
- * @param string $key The name of the cURL option
- * @param string $value The value to which the option is to be set
- * @return Builder
- */
- public function withOption($key, $value)
- {
- return $this->withCurlOption( $key, $value );
- }
- /**
- * Set any specific cURL option
- *
- * @param string $key The name of the cURL option
- * @param string $value The value to which the option is to be set
- * @return Builder
- */
- protected function withCurlOption($key, $value)
- {
- $this->curlOptions[ $key ] = $value;
- return $this;
- }
- /**
- * Set any specific package option
- *
- * @param string $key The name of the cURL option
- * @param string $value The value to which the option is to be set
- * @return Builder
- */
- protected function withPackageOption($key, $value)
- {
- $this->packageOptions[ $key ] = $value;
- return $this;
- }
- /**
- * Add a HTTP header to the request
- *
- * @param string $header The HTTP header that is to be added to the request
- * @return Builder
- */
- public function withHeader($header)
- {
- $this->curlOptions[ 'HTTPHEADER' ][] = $header;
- return $this;
- }
- /**
- * Add a content type HTTP header to the request
- *
- * @param string $contentType The content type of the file you would like to download
- * @return Builder
- */
- public function withContentType($contentType)
- {
- return $this->withHeader( 'Content-Type: '. $contentType )
- ->withHeader( 'Connection: Keep-Alive' );
- }
- /**
- * Send a GET request to a URL using the specified cURL options
- *
- * @return mixed
- */
- public function get()
- {
- $parameterString = '';
- if( is_array($this->packageOptions[ 'data' ]) && count($this->packageOptions[ 'data' ]) != 0 ) {
- $parameterString = '?'. http_build_query($this->packageOptions[ 'data' ]);
- }
- $this->curlOptions[ 'URL' ] .= $parameterString;
- return $this->send();
- }
- /**
- * Send a POST request to a URL using the specified cURL options
- *
- * @return mixed
- */
- public function post()
- {
- $this->setPostParameters();
- return $this->send();
- }
- /**
- * Send a download request to a URL using the specified cURL options
- *
- * @param string $fileName
- * @return mixed
- */
- public function download($fileName)
- {
- $this->packageOptions[ 'saveFile' ] = $fileName;
- return $this->send();
- }
- /**
- * Add POST parameters to the curlOptions array
- */
- protected function setPostParameters()
- {
- $this->curlOptions[ 'POST' ] = true;
- $parameters = $this->packageOptions[ 'data' ];
- if( $this->packageOptions[ 'asJson' ] ) {
- $parameters = json_encode($parameters);
- }
- $this->curlOptions[ 'POSTFIELDS' ] = $parameters;
- }
- // /**
- // * Send a PUT request to a URL using the specified cURL options
- // *
- // * @return mixed
- // */
- // public function put()
- // {
- // $this->setPostParameters();
- //
- // return $this->send();
- // }
- //
- // /**
- // * Send a DELETE request to a URL using the specified cURL options
- // *
- // * @return mixed
- // */
- // public function delete()
- // {
- // $this->setPostParameters();
- //
- // return $this->send();
- // }
- /**
- * Send the request
- *
- * @return mixed
- */
- protected function send()
- {
- // Add JSON header if necessary
- if( $this->packageOptions[ 'asJson' ] ) {
- $this->withHeader( 'Content-Type: application/json' );
- }
- // Create the request with all specified options
- $this->curlObject = curl_init();
- $options = $this->forgeOptions();
- curl_setopt_array( $this->curlObject, $options );
- // Send the request
- $response = curl_exec( $this->curlObject );
- curl_close( $this->curlObject );
- if( $this->packageOptions[ 'saveFile' ] ) {
- // Save to file if a filename was specified
- $file = fopen($this->packageOptions[ 'saveFile' ], 'w');
- fwrite($file, $response);
- fclose($file);
- } else if( $this->packageOptions[ 'asJson' ] ) {
- // Decode the request if necessary
- $response = json_decode($response, $this->packageOptions[ 'returnAsArray' ]);
- }
- // Return the result
- return $response;
- }
- /**
- * Convert the curlOptions to an array of usable options for the cURL request
- *
- * @return array
- */
- protected function forgeOptions()
- {
- $results = array();
- foreach( $this->curlOptions as $key => $value ) {
- $array_key = constant( 'CURLOPT_' . $key );
- if( $key == 'POSTFIELDS' && is_array( $value ) ) {
- $results[ $array_key ] = http_build_query( $value, null, '&' );
- } else {
- $results[ $array_key ] = $value;
- }
- }
- return $results;
- }
- }
|