1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: /**
13: * XoopsHttpGet - return response to a http get request
14: *
15: * @category HttpGet
16: * @package Xoops
17: * @author Richard Griffith <richard@geekwright.com>
18: * @copyright 2020 XOOPS Project (https://xoops.org)
19: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20: */
21: class XoopsHttpGet
22: {
23: protected $useCurl = true;
24: protected $url;
25: protected $error;
26:
27: /**
28: * XoopsHttpGet constructor.
29: *
30: * @param string $url the url to process
31: *
32: * @throws \RuntimeException if neither curl extension nor stream wrappers (allow_url_fopen) is available
33: */
34: public function __construct($url)
35: {
36: $this->url = $url;
37: if (!function_exists('curl_init')) {
38: $this->useCurl = false;
39: $urlFopen = (int) ini_get('allow_url_fopen');
40: if ($urlFopen === 0) {
41: throw new \RuntimeException("CURL extension or allow_url_fopen ini setting is required.");
42: }
43: }
44: }
45:
46: /**
47: * Return the response from a GET to the specified URL.
48: *
49: * @return string|bool response or false on error
50: */
51: public function fetch()
52: {
53: return ($this->useCurl) ? $this->fetchCurl() : $this->fetchFopen();
54: }
55:
56: /**
57: * Use curl to GET the specified URL.
58: *
59: * @return string|bool response or false on error
60: */
61: protected function fetchCurl()
62: {
63: $curlHandle = curl_init($this->url);
64: if (false === $curlHandle) {
65: $this->error = 'curl_init failed';
66: return false;
67: }
68: $options = array(
69: CURLOPT_RETURNTRANSFER => 1,
70: CURLOPT_HEADER => 0,
71: CURLOPT_CONNECTTIMEOUT => 10,
72: CURLOPT_FOLLOWLOCATION => 1,
73: CURLOPT_MAXREDIRS => 4,
74: );
75: curl_setopt_array($curlHandle, $options);
76:
77: $response = curl_exec($curlHandle);
78: if (false === $response) {
79: $this->error = curl_error($curlHandle);
80: } else {
81: $httpcode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
82: if (200 != $httpcode) {
83: $this->error = $response;
84: $response = false;
85: }
86: }
87: curl_close($curlHandle);
88: return $response;
89: }
90:
91: /**
92: * Use stream wrapper to GET the specified URL.
93: *
94: * @return string|false response or false on error
95: */
96: protected function fetchFopen()
97: {
98: $response = file_get_contents($this->url);
99: if (false === $response) {
100: $this->error = 'file_get_contents() failed.';
101: }
102: return $response;
103: }
104:
105: /**
106: * Return any error set during processing of fetch()
107: *
108: * @return string|null
109: */
110: public function getError()
111: {
112: return $this->error;
113: }
114: }
115: