123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- <?php namespace Ixudra\Curl;
- use stdClass;
- class Builder {
- /** @var resource $curlObject cURL request */
- protected $curlObject = null;
- /** @var array $curlOptions Array of cURL options */
- protected $curlOptions = array(
- 'RETURNTRANSFER' => true,
- 'FAILONERROR' => false,
- '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(),
- 'asJsonRequest' => false,
- 'asJsonResponse' => false,
- 'returnAsArray' => false,
- 'responseObject' => false,
- 'enableDebug' => false,
- 'containsFile' => false,
- 'debugFile' => '',
- '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 );
- }
- /**
- * Allow for redirects in the request
- *
- * @return Builder
- */
- public function allowRedirect()
- {
- return $this->withCurlOption( 'FOLLOWLOCATION', true );
- }
- /**
- * 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->asJsonRequest()
- ->asJsonResponse( $asArray );
- }
- /**
- * Configure the package to encode the request data to json before sending it to the server
- *
- * @return Builder
- */
- public function asJsonRequest()
- {
- return $this->withPackageOption( 'asJsonRequest', true );
- }
- /**
- * Configure the package to decode the request data from json to object or associative array
- *
- * @param boolean $asArray Indicates whether or not the data should be returned as an array. Default: false
- * @return Builder
- */
- public function asJsonResponse($asArray = false)
- {
- return $this->withPackageOption( 'asJsonResponse', 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 Cookie File
- *
- * @param string $cookieFile File name to read cookies from
- * @return Builder
- */
- public function setCookieFile($cookieFile)
- {
- return $this->withOption( 'COOKIEFILE', $cookieFile );
- }
- /**
- * Set Cookie Jar
- *
- * @param string $cookieJar File name to store cookies to
- * @return Builder
- */
- public function setCookieJar($cookieJar)
- {
- return $this->withOption( 'COOKIEJAR', $cookieJar );
- }
-
- /**
- * 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 multiple HTTP header at the same time to the request
- *
- * @param array $headers Array of HTTP headers that must be added to the request
- * @return Builder
- */
- public function withHeaders(array $headers)
- {
- $this->curlOptions[ 'HTTPHEADER' ] = array_merge(
- $this->curlOptions[ 'HTTPHEADER' ], $headers
- );
- 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' );
- }
- /**
- * Return a full response object with HTTP status and headers instead of only the content
- *
- * @return Builder
- */
- public function returnResponseObject()
- {
- return $this->withPackageOption( 'responseObject', true );
- }
- /**
- * Enable debug mode for the cURL request
- *
- * @param string $logFile The full path to the log file you want to use
- * @return Builder
- */
- public function enableDebug($logFile)
- {
- return $this->withPackageOption( 'enableDebug', true )
- ->withPackageOption( 'debugFile', $logFile )
- ->withOption( 'VERBOSE', true );
- }
- /**
- * Enable File sending
- *
- * @return Builder
- */
- public function containsFile()
- {
- return $this->withPackageOption( 'containsFile', true );
- }
- /**
- * Send a GET request to a URL using the specified cURL options
- *
- * @return mixed
- */
- public function get()
- {
- $this->appendDataToURL();
- 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[ 'asJsonRequest' ] ) {
- $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->withOption('CUSTOMREQUEST', 'PUT')
- ->send();
- }
- /**
- * Send a PATCH request to a URL using the specified cURL options
- *
- * @return mixed
- */
- public function patch()
- {
- $this->setPostParameters();
- return $this->withOption('CUSTOMREQUEST', 'PATCH')
- ->send();
- }
- /**
- * Send a DELETE request to a URL using the specified cURL options
- *
- * @return mixed
- */
- public function delete()
- {
- $this->appendDataToURL();
- return $this->withOption('CUSTOMREQUEST', 'DELETE')
- ->send();
- }
- /**
- * Send the request
- *
- * @return mixed
- */
- protected function send()
- {
- // Add JSON header if necessary
- if( $this->packageOptions[ 'asJsonRequest' ] ) {
- $this->withHeader( 'Content-Type: application/json' );
- }
- if( $this->packageOptions[ 'enableDebug' ] ) {
- $debugFile = fopen( $this->packageOptions[ 'debugFile' ], 'w');
- $this->withOption('STDERR', $debugFile);
- }
- // 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 );
- // Capture additional request information if needed
- $responseData = array();
- if( $this->packageOptions[ 'responseObject' ] ) {
- $responseData = curl_getinfo( $this->curlObject );
- if( curl_errno($this->curlObject) ) {
- $responseData[ 'errorMessage' ] = curl_error($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[ 'asJsonResponse' ] ) {
- // Decode the request if necessary
- $response = json_decode($response, $this->packageOptions[ 'returnAsArray' ]);
- }
- if( $this->packageOptions[ 'enableDebug' ] ) {
- fclose( $debugFile );
- }
- // Return the result
- return $this->returnResponse( $response, $responseData );
- }
- /**
- * @param mixed $content Content of the request
- * @param array $responseData Additional response information
- * @return stdClass
- */
- protected function returnResponse($content, $responseData = array())
- {
- if( !$this->packageOptions[ 'responseObject' ] ) {
- return $content;
- }
- $object = new stdClass();
- $object->content = $content;
- $object->status = $responseData[ 'http_code' ];
- if( array_key_exists('errorMessage', $responseData) ) {
- $object->error = $responseData[ 'errorMessage' ];
- }
- return $object;
- }
- /**
- * 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 ) {
- $arrayKey = constant( 'CURLOPT_' . $key );
- if( !$this->packageOptions[ 'containsFile' ] && $key == 'POSTFIELDS' && is_array( $value ) ) {
- $results[ $arrayKey ] = http_build_query( $value, null, '&' );
- } else {
- $results[ $arrayKey ] = $value;
- }
- }
- return $results;
- }
- /**
- * Append set data to the query string for GET and DELETE cURL requests
- *
- * @return string
- */
- protected function appendDataToURL()
- {
- $parameterString = '';
- if( is_array($this->packageOptions[ 'data' ]) && count($this->packageOptions[ 'data' ]) != 0 ) {
- $parameterString = '?'. http_build_query( $this->packageOptions[ 'data' ], null, '&' );
- }
- return $this->curlOptions[ 'URL' ] .= $parameterString;
- }
- }
|