PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 3.6.7
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v3.6.7
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 5 years ago HttpClientCache.php 5 years ago HttpClientInterface.php 5 years ago OembedClient.php 5 years ago
HttpClient.php
131 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 PHP/Embera',
42 ], $config);
43 }
44
45 /** inline {@inheritdoc} */
46 public function fetch($url, array $params = [])
47 {
48 if (!filter_var($url, \FILTER_VALIDATE_URL)) {
49 throw new InvalidArgumentException(sprintf('Invalid url %s', $url));
50 }
51
52 if (function_exists('curl_init') && $this->config['use_curl']) {
53 return $this->fetchWithCurl($url);
54 }
55
56 return $this->fetchWithFileGetContents($url);
57 }
58
59 /**
60 * Uses Curl to fetch data from an url
61 *
62 * @param string $url
63 * @param array $params Additional parameters for the respective part
64 * @return string
65 *
66 * @throws Exception when the returned status code is not 200 or no data was found
67 */
68 protected function fetchWithCurl($url, array $params = [])
69 {
70 // Not using array_merge here because that function reindexes numeric keys
71 $options = $params + array(
72 CURLOPT_USERAGENT => $this->config['user_agent'],
73 CURLOPT_ENCODING => '',
74 CURLOPT_FOLLOWLOCATION => true,
75 );
76
77 $options[CURLOPT_URL] = $url;
78 $options[CURLOPT_HEADER] = true;
79 $options[CURLOPT_RETURNTRANSFER] = 1;
80
81 $handler = curl_init();
82 curl_setopt_array($handler, $options);
83 $response = curl_exec($handler);
84
85 $status = curl_getinfo($handler, CURLINFO_HTTP_CODE);
86 $headerSize = curl_getinfo($handler, CURLINFO_HEADER_SIZE);
87
88 $body = substr($response, $headerSize);
89 curl_close($handler);
90
91
92 if (empty($body) || $status != '200') {
93 throw new Exception($status . ': Invalid response for ' . $url);
94 }
95
96 return $body;
97 }
98
99 /**
100 * Uses file_get_contents to fetch data from an url
101 *
102 * @param string $url
103 * @param array $params Additional parameters for the respective part
104 * @return string
105 *
106 * @throws Exception when allow_url_fopen is disabled or when no data was returned
107 */
108 protected function fetchWithFileGetContents($url, array $params = [])
109 {
110 if (!ini_get('allow_url_fopen')) {
111 throw new Exception('Could not execute http request - allow_url_fopen is disabled.');
112 }
113
114 $params = array_merge([
115 'method' => 'GET',
116 'user_agent' => $this->config['user_agent'],
117 'follow_location' => 1,
118 'max_redirects' => 20,
119 'timeout' => 40
120 ], $params);
121
122 $context = array('http' => $params);
123 if ($data = file_get_contents($url, false, stream_context_create($context))) {
124 return $data;
125 }
126
127 throw new Exception('Invalid Server Response from ' . $url);
128 }
129
130 }
131