Mercurial > SimpleWebPresenter
diff Http.inc @ 94:2370f4450983
Document functions and move a few functions to more appropriate places.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 18 Oct 2012 17:33:34 +0200 |
| parents | 8aadd7a23b68 |
| children | 111770d32c2e |
line wrap: on
line diff
--- a/Http.inc Thu Oct 18 16:44:48 2012 +0200 +++ b/Http.inc Thu Oct 18 17:33:34 2012 +0200 @@ -3,12 +3,74 @@ /// @cond $baseDir = dirname(__FILE__); - $cache = ScriptIncludeCache::instance(__FILE__); /// @endcond +/** + * Thrown if a request has gone bad + */ +class RequestException extends RuntimeException +{ + private $_info; + + /** + * Constructs a RequestException + * + * @param $info an info array as defined in http_get, if a key with + * name error exist, this will be the error text for the + * RuntimeException + */ + function __construct($info) + { + $this->_info = $info; + if (array_key_exists('error', $info)) { + parent::__construct($info['error']); + } + } + + /** + * Get the info object associated with this RequestException + * + * @return $info an info array as defined in http_get + */ + function info() + { + return $this->_info; + } +} + +/** + * Http contains a set of functions for retrieving http information + */ class Http { + /** + * Gets the content of a page + * + * This mimics the file_content with a uri parameter which is often + * disabled due to security reasons + * + * @param $uri The uri to fetch + */ + static function get_uri_contents($uri) + { + $crl = curl_init(); + $timeout = 5; + + curl_setopt ($crl, CURLOPT_URL, $uri); + curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); + $ret = curl_exec($crl); + curl_close($crl); + return $ret; + } + + /** + * Splits an http header response into an associative array + * + * @param $response The headers to parse + * @return an Associative array, the key '' refers to the HTTP status header + */ static function headersToArray($response) { $headers = array(); @@ -30,6 +92,7 @@ * Queries a URL for headers * * @param $url the url to query + * @param $timeout float number of seconds to wait before timeout * @return an associative array of all headers returned */ static function getHeaders($url, $timeout = 1) @@ -43,7 +106,15 @@ return self::headersToArray($response); } - static function postHeaders($url, $params, $timeout = 1) + /** + * Performs a post to an URI for headers + * + * @param $uri the uri to query + * @param $params an associative array of params to post + * @param $timeout float number of seconds to wait before timeout + * @return an associative array of all headers returned + */ + static function postHeaders($uri, $params, $timeout = 1) { $crl = curl_init(); @@ -57,7 +128,7 @@ //being reordered. $process = proc_open('tac | tac', $descriptorspec, $pipes); - curl_setopt ($crl, CURLOPT_URL, $url); + curl_setopt ($crl, CURLOPT_URL, $uri); curl_setopt ($crl, CURLOPT_WRITEHEADER, $pipes[0]); curl_setopt ($crl, CURLOPT_NOBODY, true); curl_setopt ($crl, CURLOPT_POST, true);
