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 |