PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / trunk
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more vtrunk
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / wpdevelopers / embera / src / Embera / Http / HttpClient.php
embedpress / vendor / wpdevelopers / embera / src / Embera / Http Last commit date
HttpClient.php 9 months ago HttpClientCache.php 5 years ago HttpClientInterface.php 9 months ago OembedClient.php 9 months ago
HttpClient.php
151 lines
1 <?php
2 /**
3 * HttpClient.php
4 *
5 * @package Embera
6 * @author Michael Pratt <yo@michael-pratt.com>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera\Http;
14
15 use Exception;
16 use InvalidArgumentException;
17
18 /**
19 * This class is in charge of doing http requests. Its a very minimal
20 * wrapper for curl or file_get_contents
21 */
22 class HttpClient implements HttpClientInterface
23 {
24 /** @var array Array with custom curl/fopen options */
25 protected $config = [];
26
27 /**
28 * Alias for the setConfig method
29 * @param array $config
30 */
31 public function __construct(array $config = [])
32 {
33 $this->setConfig($config);
34 }
35
36 /** inline {@inheritdoc} */
37 public function setConfig(array $config = [])
38 {
39 $this->config = array_merge([
40 'use_curl' => true,
41 'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
42 'referer' => '',
43 'curl_params' => [],
44 'file_get_contents_params' => []
45 ], $config);
46 }
47
48 /** inline {@inheritdoc} */
49 public function fetch($url, array $params = [])
50 {
51 if (!filter_var($url, \FILTER_VALIDATE_URL)) {
52 throw new InvalidArgumentException(sprintf('Invalid url %s', $url));
53 }
54
55 if (function_exists('curl_init') && $this->config['use_curl']) {
56 return $this->fetchWithCurl($url, $this->config['curl_params']);
57 }
58
59 return $this->fetchWithFileGetContents($url, $this->config['file_get_contents_params']);
60 }
61
62 /**
63 * Uses Curl to fetch data from an url
64 *
65 * @param string $url
66 * @param array $params Additional parameters for the respective part
67 * @return string
68 *
69 * @throws Exception when the returned status code is not 200 or no data was found
70 */
71 protected function fetchWithCurl($url, array $params = [])
72 {
73 // Not using array_merge here because that function reindexes numeric keys
74 $options = $params + array(
75 CURLOPT_USERAGENT => $this->config['user_agent'],
76 CURLOPT_ENCODING => '',
77 CURLOPT_FOLLOWLOCATION => true,
78 CURLOPT_SSL_VERIFYHOST => 0,
79 CURLOPT_SSL_VERIFYPEER => 0,
80 CURLINFO_HEADER_OUT => true,
81 );
82
83 if (!empty($this->config['referer'])) {
84 $options[CURLOPT_REFERER] = $this->config['referer'];
85 }
86
87 $options[CURLOPT_URL] = $url;
88 $options[CURLOPT_HEADER] = true;
89 $options[CURLOPT_RETURNTRANSFER] = 1;
90
91 $handler = curl_init();
92 curl_setopt_array($handler, $options);
93
94 $response = curl_exec($handler);
95
96 $status = curl_getinfo($handler, CURLINFO_HTTP_CODE);
97 $headerSize = curl_getinfo($handler, CURLINFO_HEADER_SIZE);
98 //$headerOut = curl_getinfo($handler, CURLINFO_HEADER_OUT);
99
100 $body = substr($response, $headerSize);
101 curl_close($handler);
102
103 if (empty($body) || $status != '200') {
104 throw new Exception($status . ': Invalid response for ' . $url);
105 }
106
107 return $body;
108 }
109
110 /**
111 * Uses file_get_contents to fetch data from an url
112 *
113 * @param string $url
114 * @param array $params Additional parameters for the respective part
115 * @return string
116 *
117 * @throws Exception when allow_url_fopen is disabled or when no data was returned
118 */
119 protected function fetchWithFileGetContents($url, array $params = [])
120 {
121 if (!ini_get('allow_url_fopen')) {
122 throw new Exception('Could not execute http request - allow_url_fopen is disabled.');
123 }
124
125 $params = array_merge([
126 'method' => 'GET',
127 'user_agent' => $this->config['user_agent'],
128 'follow_location' => 1,
129 'max_redirects' => 20,
130 'timeout' => 40
131 ], $params);
132
133 if (!empty($this->config['referer'])) {
134 $referer = 'Referer: ' . $this->config['referer'] . "\r\n";
135 if (!empty($params['header'])) {
136 $params['header'] = [ $referer ];
137 } else {
138 $params['header'][] = $referer;
139 }
140 }
141
142 $context = array('http' => $params);
143 if ($data = file_get_contents($url, false, stream_context_create($context))) {
144 return $data;
145 }
146
147 throw new Exception('Invalid Server Response from ' . $url);
148 }
149
150 }
151