123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <?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(),
- 'SSL_VERIFYPEER' => false,
- );
- /** @var array $packageOptions Array with options that are not specific to cURL but are used by the package */
- protected $packageOptions = array(
- 'data' => array(),
- 'files' => array(),
- 'asJsonRequest' => false,
- 'asJsonResponse' => false,
- 'returnAsArray' => false,
- 'responseObject' => false,
- 'responseArray' => false,
- 'enableDebug' => false,
- 'xDebugSessionName' => '',
- '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 float $timeout The timeout for the request (in seconds, fractions of a second are okay. Default: 30 seconds)
- * @return Builder
- */
- public function withTimeout($timeout = 30.0)
- {
- return $this->withCurlOption( 'TIMEOUT_MS', ($timeout * 1000) );
- }
- /**
- * Add GET or POST data to the request
- *
- * @param mixed $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 );
- }
- /**
- * Add a file to the request
- *
- * @param string $key Identifier of the file (how it will be referenced by the server in the $_FILES array)
- * @param string $path Full path to the file you want to send
- * @param string $mimeType Mime type of the file
- * @param string $postFileName Name of the file when sent. Defaults to file name
- *
- * @return Builder
- */
- public function withFile($key, $path, $mimeType = '', $postFileName = '')
- {
- $fileData = array(
- 'fileName' => $path,
- 'mimeType' => $mimeType,
- 'postFileName' => $postFileName,
- );
- $this->packageOptions[ 'files' ][ $key ] = $fileData;
- return $this->containsFile();
- }
- /**
- * 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 );
- }
- /**
- * Return a full response array with HTTP status and headers instead of only the content
- *
- * @return Builder
- */
- public function returnResponseArray()
- {
- return $this->withPackageOption( 'responseArray', 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 Proxy for the cURL request
- *
- * @param string $proxy Hostname
- * @param string $port Port to be used
- * @param string $type Scheme to be used by the proxy
- * @param string $username Authentication username
- * @param string $password Authentication password
- * @return Builder
- */
- public function withProxy($proxy, $port = '', $type = '', $username = '', $password = '')
- {
- $this->withOption( 'PROXY', $proxy );
- if( !empty($port) ) {
- $this->withOption( 'PROXYPORT', $port );
- }
- if( !empty($type) ) {
- $this->withOption( 'PROXYTYPE', $type );
- }
- if( !empty($username) && !empty($password) ) {
- $this->withOption( 'PROXYUSERPWD', $username .':'. $password );
- }
- return $this;
- }
- /**
- * Enable File sending
- *
- * @return Builder
- */
- public function containsFile()
- {
- return $this->withPackageOption( 'containsFile', true );
- }
- /**
- * Add the XDebug session name to the request to allow for easy debugging
- *
- * @param string $sessionName
- * @return Builder
- */
- public function enableXDebug($sessionName = 'session_1')
- {
- $this->packageOptions[ 'xDebugSessionName' ] = $sessionName;
- return $this;
- }
- /**
- * 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( !empty($this->packageOptions[ 'files' ]) ) {
- foreach( $this->packageOptions[ 'files' ] as $key => $file ) {
- $parameters[ $key ] = $this->getCurlFileValue( $file[ 'fileName' ], $file[ 'mimeType' ], $file[ 'postFileName'] );
- }
- }
- if( $this->packageOptions[ 'asJsonRequest' ] ) {
- $parameters = json_encode($parameters);
- }
- $this->curlOptions[ 'POSTFIELDS' ] = $parameters;
- }
- protected function getCurlFileValue($filename, $mimeType, $postFileName)
- {
- // PHP 5 >= 5.5.0, PHP 7
- if( function_exists('curl_file_create') ) {
- return curl_file_create($filename, $mimeType, $postFileName);
- }
- // Use the old style if using an older version of PHP
- $value = "@{$filename};filename=" . $postFileName;
- if( $mimeType ) {
- $value .= ';type=' . $mimeType;
- }
- return $value;
- }
- /**
- * 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' ] || $this->packageOptions[ 'responseArray' ] ) {
- $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 mixed
- */
- protected function returnResponse($content, array $responseData = array())
- {
- if( !$this->packageOptions[ 'responseObject' ] && !$this->packageOptions[ 'responseArray' ] ) {
- return $content;
- }
- $object = new stdClass();
- $object->content = $content;
- $object->status = $responseData[ 'http_code' ];
- $object->contentType = $responseData[ 'content_type' ];
- if( array_key_exists('errorMessage', $responseData) ) {
- $object->error = $responseData[ 'errorMessage' ];
- }
- if( $this->packageOptions[ 'responseObject' ] ) {
- return $object;
- }
- if( $this->packageOptions[ 'responseArray' ] ) {
- return (array) $object;
- }
- return $content;
- }
- /**
- * 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;
- }
- }
- if( !empty($this->packageOptions[ 'xDebugSessionName' ]) ) {
- $char = strpos($this->curlOptions[ 'URL' ], '?') ? '&' : '?';
- $this->curlOptions[ 'URL' ] .= $char . 'XDEBUG_SESSION_START='. $this->packageOptions[ 'xDebugSessionName' ];
- }
- 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;
- }
- }
|