diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13bbe5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ + diff --git a/README.md b/README.md index cb754c7..5f80d82 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Using ```php 1, - CURLOPT_HEADER => 1, -)); - -$result = curl_exec($ch); - -echo 'GET request was successful. ' . mb_strlen($result) . ' bytes received'; \ No newline at end of file diff --git a/src/Purl.php b/src/neTpyceB/Purl/Purl.php similarity index 66% rename from src/Purl.php rename to src/neTpyceB/Purl/Purl.php index 41189f0..7b5e9e0 100644 --- a/src/Purl.php +++ b/src/neTpyceB/Purl/Purl.php @@ -1,7 +1,9 @@ 'http_code', - CURLINFO_HEADER_OUT => 'request_header' + CURLINFO_HEADER_OUT => 'request_header', + CURLINFO_EFFECTIVE_URL => 'url', + CURLINFO_CONTENT_TYPE => 'content_type', ); - + /** * Setter for curl options - * + * * @param int $option CURLOPT constant * @param mixed $value option value to set * @return void @@ -103,7 +112,7 @@ class Purl public function setOption($option, $value) { $this->_options[$option] = $value; - + switch ($option) { case CURLOPT_POSTFIELDS: $this->_params = $value; @@ -114,6 +123,7 @@ public function setOption($option, $value) break; case CURLOPT_URL: $this->_url = $value; + $this->_info['url'] = $value; break; case CURLOPT_POST: $this->_method = $value ? 'POST' : $this->_method; @@ -127,160 +137,211 @@ public function setOption($option, $value) case CURLOPT_USERAGENT: $this->_agent = $value; break; + + //ignore these + case CURLOPT_HEADER: + case CURLOPT_RETURNTRANSFER: + case CURLOPT_HTTPGET: + case CURLOPT_TIMEOUT: + + //might be important + case CURLOPT_INFILESIZE: + + //was important + case CURLOPT_USERPWD: + break; + + default: + error_log('Purl choked on option ' . $option); } - + return true; } - + + /** + * Get last response number + * + * @return int + */ + public function getResponseNumber() + { + return $this->_responseno; + } + /** * Get last error number - * + * * @return int */ public function getErrorNumber() - { + { return $this->_errorno; } - + /** * Get last error message - * + * * @return type */ public function getError() - { + { return $this->_error; } - + /** - * - * + * + * * @param type $opt * @return type */ public function getInfo($opt = 0) - { + { if ($opt === 0) { - + return $this->_info; - - } - + + } + if (isset(self::$_infoMap[$opt])) { return $this->_info[self::$_infoMap[$opt]]; } - + trigger_error(__FUNCTION__ . PURL_NOT_SUPPORTED_MSG, PURL_ERROR_TYPE); } - + /** - * - * + * + * * @param type $opt * @return type */ public function getLog() - { + { return $this->_logs; } - + /** * Execute "curl" call - * + * * @return type */ public function execute() - { + { $headers = $query = ''; - - if (!empty($this->_params)) { + + if (!empty($this->_params)) { if (is_array($this->_params)) { $query = http_build_query($this->_params, null, '&'); } else { $query = $this->_params; } } - + + // Support basic authentication + if (isset($this->_options[CURLOPT_USERPWD])) { + $this->_headers[] = 'Authorization: Basic ' . base64_encode($this->_options[CURLOPT_USERPWD]); + } + // add cookie header if (isset($this->_options[CURLOPT_COOKIE])) { - $this->_headers['Cookie'] = $this->_options[CURLOPT_COOKIE]; + $this->_headers[] = 'Cookie: ' . $this->_options[CURLOPT_COOKIE]; } - + if ($this->_headers) { - foreach ($this->_headers as $name => $value) { - $headers .= $name . ': ' . $value . "\r\n"; + foreach ($this->_headers as $value) { + // status code 417 Expect: 100 continue + if (0 === strpos($value, 'Expect:')) { + continue; + } + $headers .= $value . "\r\n"; + } + if (1024 <= strlen($headers)) { + $headers .= 'Except: 100-continue' . "\r\n"; } } - - if (!isset($this->_headers['Content-type'])) $headers .= "Content-type: " . "application/x-www-form-urlencoded"."\r\n"; - + + if (!preg_match('/Content-type/', $headers)) $headers .= "Content-type: " . "application/x-www-form-urlencoded" . "\r\n"; + return $this->_call($query, $this->_info['request_header'] = $headers); } - + /** * Make request using file_get_contents. * Trying to set as much curl original settings as possible - * + * * @param string $query GET query string * @param array $headers headers to send along with request * @return boolean|string */ protected function _call($query, $headers) - { + { $options = - array('http'=> - array( - 'method' => $this->_method, - 'header' => $headers, - 'ignore_errors' => true, - 'user_agent' => $this->_agent, - ) + array('http' => + array( + 'method' => $this->_method, + 'header' => $headers, + 'ignore_errors' => true, + 'user_agent' => $this->_agent, + ) ); - + // HTTP context settings if (isset($this->_options[CURLOPT_TIMEOUT])) $options['http']['timeout'] = $this->_options[CURLOPT_TIMEOUT]; - + if (isset($this->_options[CURLOPT_FOLLOWLOCATION])) $options['http']['follow_location'] = $this->_options[CURLOPT_FOLLOWLOCATION]; - + if (isset($this->_options[CURLOPT_MAXREDIRS])) $options['http']['max_redirects'] = $this->_options[CURLOPT_MAXREDIRS]; - + if (isset($this->_options[CURLOPT_PROXY])) { $options['http']['proxy'] = $this->_options[CURLOPT_PROXY]; $options['http']['request_fulluri'] = true; } - + // SSL context settings if (isset($this->_options[CURLOPT_SSL_VERIFYPEER])) $options['ssl']['verify_peer'] = $this->_options[CURLOPT_SSL_VERIFYPEER]; if (isset($this->_options[CURLOPT_CAINFO])) $options['ssl']['cafile'] = $this->_options[CURLOPT_CAINFO]; - + if (isset($this->_options[CURLOPT_USERPWD]) && $this->_options[CURLOPT_USERPWD]) { + $url_parts = explode('://', $this->_url); + $this->_url = $url_parts[0] . '://' . $this->_options[CURLOPT_USERPWD] . '@' . $url_parts[1]; + } + if ($this->_method === 'POST') { $options['http']['content'] = $query; } elseif (!empty($query)) { $this->_url .= '?' . $query; } - + $context = stream_context_create($options); - - stream_context_set_params($context, array('notification' => function() { + + stream_context_set_params($context, array('notification' => function () { // not working yet :/ $this->_logs[] = func_get_args(); })); - + $reporting = error_reporting(0); - + if (!empty($this->_options[CURLOPT_NOBODY])) { $handler = fopen($this->_url, 'r', false, $context); } else { $this->_result = file_get_contents($this->_url, false, $context); } - - $this->_info['http_code'] = explode(' ', $http_response_header[0])[1]; - + + $httpCode = explode(' ', $http_response_header[0])[1]; + $this->_info['http_code'] = (int)$httpCode; + + // response Content Type + foreach ($http_response_header as $header) { + if (1 === preg_match('/Content\-Type/iu', $header)) { + $this->_info['content_type'] = $header; + break; + } + } + error_reporting($reporting); - + // on failure if ($this->_result === false || (isset($handler) && $handler === false)) { - + $this->_errorHandler(); return false; @@ -289,34 +350,36 @@ protected function _call($query, $headers) if (!empty($this->_options[CURLOPT_HEADER])) { $this->_result = implode($http_response_header, "\r\n") . "\r\n\r\n" . $this->_result; } - + + $this->_responseno=explode(' ',$http_response_header[0])[1]; + return isset($this->_options[CURLOPT_RETURNTRANSFER]) ? $this->_result : true; } - + /** * Handle request errors * Set error message and message number - * + * * @return void */ private function _errorHandler() { $error = error_get_last(); - + $parts = parse_url($this->_url); - + if (strpos($error['message'], 'php_network_getaddresses')) { - $this->_error = "Couldn't resolve host '" . $parts['host'] . "'"; - $this->_errorno = 6; + $this->_error = "Couldn't resolve host '" . $parts['host'] . "'"; + $this->_errorno = 6; } elseif (strpos($error['message'], 'No such file or directory')) { - $this->_error = "Protocol " . $parts['scheme'] . " not supported or disabled in libcurl"; - $this->_errorno = 1; + $this->_error = "Protocol " . $parts['scheme'] . " not supported or disabled in libcurl"; + $this->_errorno = 1; } elseif (strpos($error['message'], 'failed to open stream')) { - $this->_error = "couldn't connect to host"; - $this->_errorno = 7; + $this->_error = "couldn't connect to host"; + $this->_errorno = 7; } else { - $this->_error = "Unknown Purl error. PHP error: " . $error['message']; - $this->_errorno = 100; + $this->_error = "Unknown Purl error. PHP error: " . $error['message']; + $this->_errorno = 100; } } -} \ No newline at end of file +} diff --git a/src/constants.php b/src/neTpyceB/Purl/constants.php similarity index 100% rename from src/constants.php rename to src/neTpyceB/Purl/constants.php diff --git a/src/functions.php b/src/neTpyceB/Purl/functions.php similarity index 100% rename from src/functions.php rename to src/neTpyceB/Purl/functions.php