Mercurial > SimpleWebPresenter
view 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 source
<?php include_once 'ScriptIncludeCache.inc'; /// @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(); $response = trim($response); $str = explode("\n", $response); $headers[''] = trim($str[0]); foreach($str as $kv) { $p = strpos($kv, ":"); if ($p) { $key = substr($kv, 0, $p); $value = trim(substr($kv, $p + 1)); $headers[$key] = $value; } } return $headers; } /** * 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) { $response = @http_head($url, array("timeout" => $timeout), $info); if (array_key_exists('error', $info) && $info['error']) { throw new RequestException($info); } return self::headersToArray($response); } /** * 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(); $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), //2 => array("file", "/tmp/error-output.txt", "a") ); //We use tac, since it buffers, and we don't care about the output //being reordered. $process = proc_open('tac | tac', $descriptorspec, $pipes); 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); curl_setopt ($crl, CURLOPT_POSTFIELDS, $params); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); fclose($pipes[0]); $buf = fread($pipes[1], 8192); return self::headersToArray($buf); } } ?>
