Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // <?php
- function uncompress($content, $encoding) {
- switch ($encoding) {
- case 'gzip':
- return gzdecode($content);
- case 'deflate':
- return gzinflate($content);
- }
- return $content;
- }
- /**
- * Отправляет http-запрос от имени сервера.
- *
- * @param string $method
- * @param string $url
- * @param array $options['headers'] дополнительные заголовки $name => $value
- * @param array $options['allowed_statuses'] список разрешенных кодов состояния
- * @param string $options['body'] тело запроса
- * @param float $options['timeout'] тайм-аут соединения в секундах
- * @return mixed
- */
- function proxy_request( $method, $url, $options = array() ) {
- $method = strtoupper($method);
- $parts = @parse_url($url);
- if ($parts === false) {
- die('url malformed');
- }
- $scheme = isset($parts['scheme']) ? strtolower($parts['scheme']) : 'http';
- if ($scheme != 'http' && $scheme != 'https') {
- die('only http and https are allowed');
- }
- $protocol = $scheme == 'http' ? 'tcp' : 'ssl';
- $host = strtolower( isset($parts['host']) ? $parts['host'] : getenv('HTTP_HOST') );
- $port = isset($parts['port']) ? $parts['port'] : ($protocol == 'tcp' ? 80 : 443);
- $uri = (isset($parts['path']) ? $parts['path'] : '/') . (isset($parts['query']) ? '?' . $parts['query'] : '');
- $request = "$method $uri HTTP/1.1\r\n";
- $request .= "Host: $host\r\n";
- $headers = isset($options['headers']) ? $options['headers'] : array();
- foreach ($headers as $k => $v) {
- $request .= "$k: $v\r\n";
- }
- $request .= "Connection: close\r\n\r\n";
- if ( isset($options['body']) ) {
- $request .= $options['body'];
- }
- $timeout = isset($options['timeout']) ? (float) $options['timeout'] : ini_get('default_socket_timeout');
- $fp = @fsockopen("$protocol://$host", $port, $errno, $errstr, $timeout);
- if (!$fp) {
- die("could not connect: $errstr ($errno)");
- }
- fwrite($fp, $request);
- $cur = fgets($fp);
- if ( !preg_match('#^HTTP/(1\.[01]) ([1-5]\d{2})(.*)$#', $cur, $matches) ) {
- die('bad response');
- }
- if ( isset($options['allowed_statuses']) && !in_array($matches[2], $options['allowed_statuses']) ) {
- die('bad status code');
- }
- $response = array();
- $response['scheme'] = $scheme;
- $response['host'] = $host;
- $response['port'] = $port;
- $response['uri'] = $uri;
- $response['http_version'] = $matches[1];
- $response['status'] = $matches[2];
- $response['status_text'] = trim($matches[3]);
- $headers = array();
- while ( !feof($fp) ) {
- $line = rtrim( fgets($fp) );
- if ($line === '') {
- break;
- }
- list($name, $value) = explode(': ', $line, 2);
- $name = strtolower($name);
- if ($name == 'set-cookie') {
- if ( !isset($headers[$name]) ) {
- $headers[$name] = array();
- }
- $headers[$name][] = $value;
- }
- else if ( isset($headers[$name]) ) {
- $headers[$name] .= " ,$value";
- }
- else {
- $headers[$name] = $value;
- }
- }
- $body = '';
- $transfer_encoding = isset($headers['transfer-encoding']) ? strtolower($headers['transfer-encoding']) : '';
- $content_encoding = isset($headers['content-encoding']) ? strtolower($headers['content-encoding']) : '';
- $content_length = @$headers['content-length'];
- if ($transfer_encoding == 'chunked') {
- while ( !feof($fp) ) {
- $block = rtrim( fgets($fp) );
- $size = (int) base_convert($block, 16, 10);
- // fread почему-то не читает строку указанного размера
- $chunk = stream_get_contents($fp, $size);
- $body .= uncompress($chunk, $content_encoding);
- // пропускаем \r\n
- fread($fp, 2);
- }
- }
- else {
- if ( is_int($content_length) ) {
- $body = stream_get_contents($fp, $content_length);
- }
- else {
- while ( !feof($fp) ) {
- $body .= fread($fp, 4096);
- }
- }
- $body = uncompress($body, $content_encoding);
- }
- $response['headers'] = $headers;
- $response['body'] = $body;
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment