get rid of the need for allow_url_fopen
Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
60
lib/classes/cURL/class.HttpClient.php
Normal file
60
lib/classes/cURL/class.HttpClient.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
class HttpClient
|
||||
{
|
||||
|
||||
/**
|
||||
* Executes simple GET request
|
||||
*
|
||||
* @param string $url
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function urlGet($url)
|
||||
{
|
||||
include FROXLOR_INSTALL_DIR . '/lib/version.inc.php';
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Froxlor/' . $version);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
$output = curl_exec($ch);
|
||||
if ($output === false) {
|
||||
$e = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \Exception("Curl error: " . $e);
|
||||
}
|
||||
curl_close($ch);
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads and stores a file from an url
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $target
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function fileGet($url, $target)
|
||||
{
|
||||
include FROXLOR_INSTALL_DIR . '/lib/version.inc.php';
|
||||
$fh = fopen($target, 'w');
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Froxlor/' . $version);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
|
||||
//give curl the file pointer so that it can write to it
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_FILE, $fh);
|
||||
$output = curl_exec($ch);
|
||||
if ($output === false) {
|
||||
$e = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \Exception("Curl error: " . $e);
|
||||
}
|
||||
curl_close($ch);
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user