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) {
|
public static function loadUrl($url, callable $function = null) {
|
||||||
$request = static::newRequest($url);
|
$request = static::newRequest($url);
|
||||||
$response = $request->send();
|
$response = $request->send();
|
||||||
if (!is_null($function)) {
|
if ($function) {
|
||||||
$content = $response->getContent();
|
static::performCallback($response, $function);
|
||||||
$error = $response->getError()->getMessage();
|
|
||||||
call_user_func($function, $content, $error);
|
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
@ -41,7 +39,9 @@ abstract class WebReader {
|
|||||||
*/
|
*/
|
||||||
protected static function newRequest($url) {
|
protected static function newRequest($url) {
|
||||||
$request = new Request($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_CRLF, true) // linux line feed
|
||||||
->set(CURLOPT_ENCODING, '') // accept encoding
|
->set(CURLOPT_ENCODING, '') // accept encoding
|
||||||
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
|
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
|
||||||
@ -50,6 +50,18 @@ abstract class WebReader {
|
|||||||
return $request;
|
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
|
* Load a URL via POST
|
||||||
*
|
*
|
||||||
@ -65,10 +77,8 @@ abstract class WebReader {
|
|||||||
$request->getOptions()->set(CURLOPT_POSTFIELDS, $content); // post content field
|
$request->getOptions()->set(CURLOPT_POSTFIELDS, $content); // post content field
|
||||||
}
|
}
|
||||||
$response = $request->send();
|
$response = $request->send();
|
||||||
if (!is_null($function)) {
|
if ($function) {
|
||||||
$content = $response->getContent();
|
static::performCallback($response, $function);
|
||||||
$error = $response->getError()->getMessage();
|
|
||||||
call_user_func($function, $content, $error);
|
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user