]> BookStack Code Mirror - bookstack/blob - app/Uploads/HttpFetcher.php
Notifications: Switched testing from string to reference levels
[bookstack] / app / Uploads / HttpFetcher.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Exceptions\HttpFetchException;
6
7 class HttpFetcher
8 {
9     /**
10      * Fetch content from an external URI.
11      *
12      * @param string $uri
13      *
14      * @throws HttpFetchException
15      *
16      * @return bool|string
17      */
18     public function fetch(string $uri)
19     {
20         $ch = curl_init();
21         curl_setopt_array($ch, [
22             CURLOPT_URL            => $uri,
23             CURLOPT_RETURNTRANSFER => 1,
24             CURLOPT_CONNECTTIMEOUT => 5,
25         ]);
26
27         $data = curl_exec($ch);
28         $err = curl_error($ch);
29         curl_close($ch);
30
31         if ($err) {
32             $errno = curl_errno($ch);
33             throw new HttpFetchException($err, $errno);
34         }
35
36         return $data;
37     }
38 }