2014-06-27 00:38:56 +02:00
|
|
|
<?php
|
|
|
|
|
2014-06-27 01:06:11 +02:00
|
|
|
namespace ManiaControl\Utils;
|
2014-06-27 00:38:56 +02:00
|
|
|
|
|
|
|
use cURL\Request;
|
|
|
|
use cURL\Response;
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reader Utility Class for efficient Web Requests
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2016-05-04 09:57:31 +02:00
|
|
|
* @copyright 2014-2016 ManiaControl Team
|
2014-06-27 00:38:56 +02:00
|
|
|
* @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
|
|
|
|
*/
|
2014-08-25 15:25:09 +02:00
|
|
|
public static function getUrl($url, callable $function = null) {
|
2014-06-27 00:38:56 +02:00
|
|
|
$request = static::newRequest($url);
|
|
|
|
$response = $request->send();
|
2014-08-25 15:20:14 +02:00
|
|
|
if ($function) {
|
|
|
|
static::performCallback($response, $function);
|
2014-06-27 00:38:56 +02:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2014-08-25 15:25:09 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @see WebReader::getUrl()
|
|
|
|
*/
|
|
|
|
public static function loadUrl($url, callable $function = null) {
|
|
|
|
if ($function) {
|
|
|
|
return static::getUrl($url, $function);
|
|
|
|
}
|
|
|
|
return static::getUrl($url);
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:38:56 +02:00
|
|
|
/**
|
|
|
|
* Create a new cURL Request for the given URL
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return Request
|
|
|
|
*/
|
|
|
|
protected static function newRequest($url) {
|
|
|
|
$request = new Request($url);
|
2014-08-25 15:20:14 +02:00
|
|
|
$options = $request->getOptions();
|
|
|
|
$options->set(CURLOPT_TIMEOUT, 5) // timeout
|
|
|
|
->set(CURLOPT_HEADER, false) // don't display response header
|
2014-06-27 00:38:56 +02:00
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
2014-08-25 15:20:14 +02:00
|
|
|
/**
|
|
|
|
* Perform the given callback function with the response
|
|
|
|
*
|
|
|
|
* @param Response $response
|
|
|
|
* @param callable $function
|
|
|
|
*/
|
|
|
|
protected static function performCallback(Response $response, callable $function) {
|
|
|
|
$content = $response->getContent();
|
|
|
|
$error = $response->getError()->getMessage();
|
|
|
|
call_user_func($function, $content, $error);
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:38:56 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2014-06-27 01:11:47 +02:00
|
|
|
$request = static::newRequest($url);
|
2014-08-13 11:05:52 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_POST, true); // post method
|
2014-06-27 00:38:56 +02:00
|
|
|
if ($content) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$request->getOptions()->set(CURLOPT_POSTFIELDS, $content); // post content field
|
2014-06-27 00:38:56 +02:00
|
|
|
}
|
|
|
|
$response = $request->send();
|
2014-08-25 15:20:14 +02:00
|
|
|
if ($function) {
|
|
|
|
static::performCallback($response, $function);
|
2014-06-27 00:38:56 +02:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|