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