From 8e44f0f3c5b6b76aaa521a0fb80b27b1b6c9f7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Fri, 27 Jun 2014 00:38:56 +0200 Subject: [PATCH] web reader for synchronous web requests --- application/core/Files/WebReader.php | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 application/core/Files/WebReader.php diff --git a/application/core/Files/WebReader.php b/application/core/Files/WebReader.php new file mode 100644 index 00000000..e0821bb6 --- /dev/null +++ b/application/core/Files/WebReader.php @@ -0,0 +1,79 @@ + + * @copyright 2014 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +abstract class WebReader { + + /** + * Load a URL via GET + * + * @param string $url + * @param callable $function + * @return Response + */ + public static function loadUrl($url, callable $function = null) { + $request = static::newRequest($url); + $response = $request->send(); + if (!is_null($function)) { + $content = $response->getContent(); + $error = $response->getError() + ->getMessage(); + call_user_func($function, $content, $error); + } + return $response; + } + + /** + * Create a new cURL Request for the given URL + * + * @param string $url + * @return Request + */ + protected static function newRequest($url) { + $request = new Request($url); + $request->getOptions() + ->set(CURLOPT_TIMEOUT, 10) + ->set(CURLOPT_HEADER, false) // don't display response header + ->set(CURLOPT_CRLF, true) // linux line feed + ->set(CURLOPT_ENCODING, '') // accept encoding + ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent + ->set(CURLOPT_RETURNTRANSFER, true) // return instead of output content + ->set(CURLOPT_AUTOREFERER, true); // follow redirects + return $request; + } + + /** + * Load a URL via POST + * + * @param string $url + * @param string $content + * @param callable $function + * @return Response + */ + public static function postUrl($url, $content = null, callable $function = null) { + $request = static::newRequest($url) + ->set(CURLOPT_POST, true); // post method + if ($content) { + $request->set(CURLOPT_POSTFIELDS, $content); // post content field + } + $response = $request->send(); + if (!is_null($function)) { + $content = $response->getContent(); + $error = $response->getError() + ->getMessage(); + call_user_func($function, $content, $error); + } + return $response; + } +}