2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Uploads;
|
2018-12-23 16:34:38 +01:00
|
|
|
|
|
|
|
use BookStack\Exceptions\HttpFetchException;
|
|
|
|
|
|
|
|
class HttpFetcher
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Fetch content from an external URI.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2018-12-23 16:34:38 +01:00
|
|
|
* @param string $uri
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2018-12-23 16:34:38 +01:00
|
|
|
* @throws HttpFetchException
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
|
|
|
* @return bool|string
|
2018-12-23 16:34:38 +01:00
|
|
|
*/
|
|
|
|
public function fetch(string $uri)
|
|
|
|
{
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt_array($ch, [
|
2021-06-26 17:23:15 +02:00
|
|
|
CURLOPT_URL => $uri,
|
2018-12-23 16:34:38 +01:00
|
|
|
CURLOPT_RETURNTRANSFER => 1,
|
2021-06-26 17:23:15 +02:00
|
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
2018-12-23 16:34:38 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$data = curl_exec($ch);
|
|
|
|
$err = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($err) {
|
|
|
|
throw new HttpFetchException($err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2019-01-27 11:29:23 +01:00
|
|
|
}
|