Mercurial > SimpleWebPresenter
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 93:8aadd7a23b68 | 94:2370f4450983 |
|---|---|
| 1 <?php | 1 <?php |
| 2 include_once 'ScriptIncludeCache.inc'; | 2 include_once 'ScriptIncludeCache.inc'; |
| 3 | 3 |
| 4 /// @cond | 4 /// @cond |
| 5 $baseDir = dirname(__FILE__); | 5 $baseDir = dirname(__FILE__); |
| 6 | |
| 7 $cache = ScriptIncludeCache::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 8 /// @endcond | 7 /// @endcond |
| 9 | 8 |
| 9 /** | |
| 10 * Thrown if a request has gone bad | |
| 11 */ | |
| 12 class RequestException extends RuntimeException | |
| 13 { | |
| 14 private $_info; | |
| 15 | |
| 16 /** | |
| 17 * Constructs a RequestException | |
| 18 * | |
| 19 * @param $info an info array as defined in http_get, if a key with | |
| 20 * name error exist, this will be the error text for the | |
| 21 * RuntimeException | |
| 22 */ | |
| 23 function __construct($info) | |
| 24 { | |
| 25 $this->_info = $info; | |
| 26 if (array_key_exists('error', $info)) { | |
| 27 parent::__construct($info['error']); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Get the info object associated with this RequestException | |
| 33 * | |
| 34 * @return $info an info array as defined in http_get | |
| 35 */ | |
| 36 function info() | |
| 37 { | |
| 38 return $this->_info; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * Http contains a set of functions for retrieving http information | |
| 44 */ | |
| 10 class Http | 45 class Http |
| 11 { | 46 { |
| 47 /** | |
| 48 * Gets the content of a page | |
| 49 * | |
| 50 * This mimics the file_content with a uri parameter which is often | |
| 51 * disabled due to security reasons | |
| 52 * | |
| 53 * @param $uri The uri to fetch | |
| 54 */ | |
| 55 static function get_uri_contents($uri) | |
| 56 { | |
| 57 $crl = curl_init(); | |
| 58 $timeout = 5; | |
| 59 | |
| 60 curl_setopt ($crl, CURLOPT_URL, $uri); | |
| 61 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
| 62 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
| 63 $ret = curl_exec($crl); | |
| 64 curl_close($crl); | |
| 65 return $ret; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Splits an http header response into an associative array | |
| 70 * | |
| 71 * @param $response The headers to parse | |
| 72 * @return an Associative array, the key '' refers to the HTTP status header | |
| 73 */ | |
| 12 static function headersToArray($response) | 74 static function headersToArray($response) |
| 13 { | 75 { |
| 14 $headers = array(); | 76 $headers = array(); |
| 15 $response = trim($response); | 77 $response = trim($response); |
| 16 $str = explode("\n", $response); | 78 $str = explode("\n", $response); |
| 28 | 90 |
| 29 /** | 91 /** |
| 30 * Queries a URL for headers | 92 * Queries a URL for headers |
| 31 * | 93 * |
| 32 * @param $url the url to query | 94 * @param $url the url to query |
| 95 * @param $timeout float number of seconds to wait before timeout | |
| 33 * @return an associative array of all headers returned | 96 * @return an associative array of all headers returned |
| 34 */ | 97 */ |
| 35 static function getHeaders($url, $timeout = 1) | 98 static function getHeaders($url, $timeout = 1) |
| 36 { | 99 { |
| 37 $response = @http_head($url, array("timeout" => $timeout), $info); | 100 $response = @http_head($url, array("timeout" => $timeout), $info); |
| 41 } | 104 } |
| 42 | 105 |
| 43 return self::headersToArray($response); | 106 return self::headersToArray($response); |
| 44 } | 107 } |
| 45 | 108 |
| 46 static function postHeaders($url, $params, $timeout = 1) | 109 /** |
| 110 * Performs a post to an URI for headers | |
| 111 * | |
| 112 * @param $uri the uri to query | |
| 113 * @param $params an associative array of params to post | |
| 114 * @param $timeout float number of seconds to wait before timeout | |
| 115 * @return an associative array of all headers returned | |
| 116 */ | |
| 117 static function postHeaders($uri, $params, $timeout = 1) | |
| 47 { | 118 { |
| 48 $crl = curl_init(); | 119 $crl = curl_init(); |
| 49 | 120 |
| 50 $descriptorspec = array( | 121 $descriptorspec = array( |
| 51 0 => array("pipe", "r"), | 122 0 => array("pipe", "r"), |
| 55 | 126 |
| 56 //We use tac, since it buffers, and we don't care about the output | 127 //We use tac, since it buffers, and we don't care about the output |
| 57 //being reordered. | 128 //being reordered. |
| 58 $process = proc_open('tac | tac', $descriptorspec, $pipes); | 129 $process = proc_open('tac | tac', $descriptorspec, $pipes); |
| 59 | 130 |
| 60 curl_setopt ($crl, CURLOPT_URL, $url); | 131 curl_setopt ($crl, CURLOPT_URL, $uri); |
| 61 curl_setopt ($crl, CURLOPT_WRITEHEADER, $pipes[0]); | 132 curl_setopt ($crl, CURLOPT_WRITEHEADER, $pipes[0]); |
| 62 curl_setopt ($crl, CURLOPT_NOBODY, true); | 133 curl_setopt ($crl, CURLOPT_NOBODY, true); |
| 63 curl_setopt ($crl, CURLOPT_POST, true); | 134 curl_setopt ($crl, CURLOPT_POST, true); |
| 64 curl_setopt ($crl, CURLOPT_POSTFIELDS, $params); | 135 curl_setopt ($crl, CURLOPT_POSTFIELDS, $params); |
| 65 | 136 |
