[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Utils\Http\Clients; 9 10 use s9e\TextFormatter\Utils\Http\Client; 11 12 class Native extends Client 13 { 14 /** 15 * @var bool Whether to use gzip encoding 16 */ 17 public $gzipEnabled; 18 19 /** 20 * Constructor 21 */ 22 public function __construct() 23 { 24 $this->gzipEnabled = extension_loaded('zlib'); 25 } 26 27 /** 28 * {@inheritdoc} 29 */ 30 public function get($url, array $options = []) 31 { 32 return $this->request('GET', $url, $options); 33 } 34 35 /** 36 * {@inheritdoc} 37 */ 38 public function post($url, array $options = [], $body = '') 39 { 40 return $this->request('POST', $url, $options, $body); 41 } 42 43 /** 44 * Create a stream context for given request 45 * 46 * @param string $method Request method 47 * @param array $options Request options 48 * @param string $body Request body 49 * @return resource 50 */ 51 protected function createContext($method, array $options, $body) 52 { 53 $contextOptions = [ 54 'ssl' => ['verify_peer' => $this->sslVerifyPeer], 55 'http' => [ 56 'method' => $method, 57 'timeout' => $this->timeout, 58 'header' => $this->generateHeaders($options, $body), 59 'content' => $body 60 ] 61 ]; 62 63 return stream_context_create($contextOptions); 64 } 65 66 /** 67 * Decompress given page if applicable 68 * 69 * @param string $content Response body, potentially compressed 70 * @return string Response body, uncompressed 71 */ 72 protected function decompress($content) 73 { 74 if ($this->gzipEnabled && substr($content, 0, 2) === "\x1f\x8b") 75 { 76 return gzdecode($content); 77 } 78 79 return $content; 80 } 81 82 /** 83 * Generate a list of headers for given request 84 * 85 * @param array $options Request options 86 * @param string $body Request body 87 * @return string[] 88 */ 89 protected function generateHeaders(array $options, $body) 90 { 91 $options += ['headers' => []]; 92 if ($this->gzipEnabled) 93 { 94 $options['headers'][] = 'Accept-Encoding: gzip'; 95 } 96 $options['headers'][] = 'Content-Length: ' . strlen($body); 97 98 return $options['headers']; 99 } 100 101 /** 102 * Execute an HTTP request 103 * 104 * @param string $method Request method 105 * @param string $url Request URL 106 * @param array $options Request options 107 * @param string $body Request body 108 * @return string|bool Response body or FALSE 109 */ 110 protected function request($method, $url, array $options, $body = '') 111 { 112 $response = @file_get_contents($url, false, $this->createContext($method, $options, $body)); 113 if ($response === false) 114 { 115 return false; 116 } 117 118 $response = $this->decompress($response); 119 if (!empty($options['returnHeaders'])) 120 { 121 $response = implode("\r\n", $http_response_header) . "\r\n\r\n" . $response; 122 } 123 124 return $response; 125 } 126 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |