web reader coding improvements by extracting method
This commit is contained in:
parent
bdf503d7fc
commit
72e16d63c8
@ -25,10 +25,8 @@ abstract class WebReader {
|
||||
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);
|
||||
if ($function) {
|
||||
static::performCallback($response, $function);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
@ -41,7 +39,9 @@ abstract class WebReader {
|
||||
*/
|
||||
protected static function newRequest($url) {
|
||||
$request = new Request($url);
|
||||
$request->getOptions()->set(CURLOPT_TIMEOUT, 10)->set(CURLOPT_HEADER, false) // don't display response header
|
||||
$options = $request->getOptions();
|
||||
$options->set(CURLOPT_TIMEOUT, 5) // timeout
|
||||
->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
|
||||
@ -50,6 +50,18 @@ abstract class WebReader {
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a URL via POST
|
||||
*
|
||||
@ -65,10 +77,8 @@ abstract class WebReader {
|
||||
$request->getOptions()->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);
|
||||
if ($function) {
|
||||
static::performCallback($response, $function);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user