/** * Test Http functions. */ function test_functions() { http_cache_last_modified(); http_chunked_decode(); http_deflate(); http_inflate(); http_build_cookie(); http_date(); http_get_request_body_stream(); http_get_request_body(); http_get_request_headers(); http_match_etag(); http_match_modified(); http_match_request_header(); http_support(); http_negotiate_charset(); http_negotiate_content_type(); http_negotiate_language(); ob_deflatehandler(); ob_etaghandler(); ob_inflatehandler(); http_parse_cookie(); http_parse_headers(); http_parse_message(); http_parse_params(); http_persistent_handles_clean(); http_persistent_handles_count(); http_persistent_handles_ident(); http_get(); http_head(); http_post_data(); http_post_fields(); http_put_data(); http_put_file(); http_put_stream(); http_request_body_encode(); http_request_method_exists(); http_request_method_name(); http_request_method_register(); http_request_method_unregister(); http_request(); http_redirect(); http_send_content_disposition(); http_send_content_type(); http_send_data(); http_send_file(); http_send_last_modified(); http_send_status(); http_send_stream(); http_throttle(); http_build_str(); http_build_url(); }
/** * Send a HTTP request to a URI using HTTP extension. * * Does not support non-blocking. * * @param string $url The URL to handle. * @param str|array $args Optional. Override the defaults. * * @return array 'headers', 'body', 'cookies' and 'response' keys. */ protected function _doExecute($url, $args) { switch ($args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD]) { case org_tubepress_api_http_HttpClient::HTTP_METHOD_POST: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_POST; break; case org_tubepress_api_http_HttpClient::HTTP_METHOD_PUT: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_PUT; break; case org_tubepress_api_http_HttpClient::HTTP_METHOD_GET: default: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_GET; } $urlAsArray = parse_url($url); if ('http' != $urlAsArray['scheme'] && 'https' != $urlAsArray['scheme']) { $url = preg_replace('|^' . preg_quote($urlAsArray['scheme'], '|') . '|', 'http', $url); } $sslVerify = isset($args[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]) && $args[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]; $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT] = (int) ceil($args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT]); $options = array('timeout' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT], 'connecttimeout' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT], 'redirect' => 5, 'useragent' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_USER_AGENT], 'headers' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS], 'ssl' => array('verifypeer' => $sslVerify, 'verifyhost' => $sslVerify)); $strResponse = @http_request($args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD], $url, $args[org_tubepress_impl_http_HttpClientChain::ARGS_BODY], $options, $info); // Error may still be set, Response may return headers or partial document, and error // contains a reason the request was aborted, eg, timeout expired or max-redirects reached. if (false === $strResponse || !empty($info['error'])) { throw new Exception($info['response_code'] . ': ' . $info['error']); } $headersBody = self::_breakRawStringResponseIntoHeaderAndBody($strResponse); $theHeaders = $headersBody[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS]; $theBody = $headersBody[org_tubepress_impl_http_HttpClientChain::ARGS_BODY]; unset($headersBody); $theHeaders = self::_getProcessedHeaders($theHeaders); if (!empty($theBody) && isset($theHeaders['headers']['transfer-encoding']) && 'chunked' == $theHeaders['headers']['transfer-encoding']) { $theBody = @http_chunked_decode($theBody); } if (true === $args['decompress'] && true === org_tubepress_impl_http_clientimpl_Encoding::shouldDecode($theHeaders['headers'])) { $theBody = http_inflate($theBody); } return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theHeaders['response'], 'cookies' => $theHeaders['cookies']); }
<?php $title = get_input('title'); $endpoint = 'http://api.stackoverflow.com/1.1/similar?type=jsontext&title=' . urlencode($title); echo http_inflate(file_get_contents($endpoint));
/** * Send a HTTP request to a URI using HTTP extension. * * Does not support non-blocking. * * @access public * @since 2.7 * * @param string $url * @param str|array $args Optional. Override the defaults. * @return array 'headers', 'body', 'cookies' and 'response' keys. */ function request($url, $args = array()) { $defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array()); $r = wp_parse_args($args, $defaults); if (isset($r['headers']['User-Agent'])) { $r['user-agent'] = $r['headers']['User-Agent']; unset($r['headers']['User-Agent']); } else { if (isset($r['headers']['user-agent'])) { $r['user-agent'] = $r['headers']['user-agent']; unset($r['headers']['user-agent']); } } // Construct Cookie: header if any cookies are set WP_Http::buildCookieHeader($r); switch ($r['method']) { case 'POST': $r['method'] = HTTP_METH_POST; break; case 'HEAD': $r['method'] = HTTP_METH_HEAD; break; case 'PUT': $r['method'] = HTTP_METH_PUT; break; case 'GET': default: $r['method'] = HTTP_METH_GET; } $arrURL = parse_url($url); if ('http' != $arrURL['scheme'] || 'https' != $arrURL['scheme']) { $url = preg_replace('|^' . preg_quote($arrURL['scheme'], '|') . '|', 'http', $url); } $is_local = isset($args['local']) && $args['local']; $ssl_verify = isset($args['sslverify']) && $args['sslverify']; if ($is_local) { $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify); } elseif (!$is_local) { $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); } $options = array('timeout' => $r['timeout'], 'connecttimeout' => $r['timeout'], 'redirect' => $r['redirection'], 'useragent' => $r['user-agent'], 'headers' => $r['headers'], 'ssl' => array('verifypeer' => $ssl_verify, 'verifyhost' => $ssl_verify)); // The HTTP extensions offers really easy proxy support. $proxy = new WP_HTTP_Proxy(); if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) { $options['proxyhost'] = $proxy->host(); $options['proxyport'] = $proxy->port(); $options['proxytype'] = HTTP_PROXY_HTTP; if ($proxy->use_authentication()) { $options['proxyauth'] = $proxy->authentication(); $options['proxyauthtype'] = HTTP_AUTH_BASIC; } } if (!WP_DEBUG) { //Emits warning level notices for max redirects and timeouts $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info); } else { $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); } //Emits warning level notices for max redirects and timeouts // Error may still be set, Response may return headers or partial document, and error // contains a reason the request was aborted, eg, timeout expired or max-redirects reached. if (false === $strResponse || !empty($info['error'])) { return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']); } if (!$r['blocking']) { return array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array()); } list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2); $theHeaders = WP_Http::processHeaders($theHeaders); if (!empty($theBody) && isset($theHeaders['headers']['transfer-encoding']) && 'chunked' == $theHeaders['headers']['transfer-encoding']) { if (!WP_DEBUG) { $theBody = @http_chunked_decode($theBody); } else { $theBody = http_chunked_decode($theBody); } } if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers'])) { $theBody = http_inflate($theBody); } $theResponse = array(); $theResponse['code'] = $info['response_code']; $theResponse['message'] = get_status_header_desc($info['response_code']); return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies']); }
/** * Send a HTTP request to a URI using HTTP extension. * * Does not support non-blocking. * * @access public * @since 2.7 * * @param string $url * @param str|array $args Optional. Override the defaults. * @return array 'headers', 'body', and 'response' keys. */ function request($url, $args = array()) { $defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null); $r = wp_parse_args($args, $defaults); if (isset($r['headers']['User-Agent'])) { $r['user-agent'] = $r['headers']['User-Agent']; unset($r['headers']['User-Agent']); } else { if (isset($r['headers']['user-agent'])) { $r['user-agent'] = $r['headers']['user-agent']; unset($r['headers']['user-agent']); } } switch ($r['method']) { case 'POST': $r['method'] = HTTP_METH_POST; break; case 'HEAD': $r['method'] = HTTP_METH_HEAD; break; case 'GET': default: $r['method'] = HTTP_METH_GET; } $arrURL = parse_url($url); if ('http' != $arrURL['scheme'] || 'https' != $arrURL['scheme']) { $url = str_replace($arrURL['scheme'], 'http', $url); } $options = array('timeout' => $r['timeout'], 'connecttimeout' => $r['timeout'], 'redirect' => $r['redirection'], 'useragent' => $r['user-agent'], 'headers' => $r['headers']); if (!defined('WP_DEBUG') || defined('WP_DEBUG') && false === WP_DEBUG) { //Emits warning level notices for max redirects and timeouts $strResponse = @http_request($r['method'], $url, $r['body'], $options, $info); } else { $strResponse = http_request($r['method'], $url, $r['body'], $options, $info); } //Emits warning level notices for max redirects and timeouts if (false === $strResponse || !empty($info['error'])) { //Error may still be set, Response may return headers or partial document, and error contains a reason the request was aborted, eg, timeout expired or max-redirects reached return new WP_Error('http_request_failed', $info['response_code'] . ': ' . $info['error']); } if (!$r['blocking']) { return array('headers' => array(), 'body' => '', 'response' => array('code', 'message')); } list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2); $theHeaders = WP_Http::processHeaders($theHeaders); if (!empty($theBody) && isset($theHeaders['headers']['transfer-encoding']) && 'chunked' == $theHeaders['headers']['transfer-encoding']) { if (!defined('WP_DEBUG') || defined('WP_DEBUG') && false === WP_DEBUG) { $theBody = @http_chunked_decode($theBody); } else { $theBody = http_chunked_decode($theBody); } } if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders)) { $theBody = http_inflate($theBody); } $theResponse = array(); $theResponse['code'] = $info['response_code']; $theResponse['message'] = get_status_header_desc($info['response_code']); return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse); }
/** * Gzip圧縮されたレスポンスボディをデコードする * * @param string $body * @param string $caller * @return string */ protected static function _decodeGzip($body, $url) { global $_conf; if (function_exists('http_inflate')) { // pecl_http の http_inflate() で展開 $body = http_inflate($body); } else { // gzip tempファイルに保存・PHPで解凍読み込み if (!is_dir($_conf['tmp_dir'])) { FileCtl::mkdirRecursive($_conf['tmp_dir']); } $gztempfile = tempnam($_conf['tmp_dir'], 'gz_'); if (false === $gztempfile) { p2die('一時ファイルを作成できませんでした。'); } if (false === file_put_contents($gztempfile, $body)) { unlink($gztempfile); p2die('一時ファイルに書き込めませんでした。'); } $body = file_get_contents('compress.zlib://' . $gztempfile); if (false === $body) { $body = null; } unlink($gztempfile); } if (is_null($body)) { $summary = 'gzip展開エラー'; $description = self::_urlToAnchor($url) . ' をgzipデコードできませんでした。'; self::_pushInfoMessage($summary, $description); } return $body; }
<?php // only logged in users can add blog posts // gatekeeper $question_id = get_input('question_id'); $endpoint = 'http://api.stackoverflow.com/1.1/questions/' . $question_id . '?type=jsontext&body=true&answers=true&comments=true'; $response = json_decode(http_inflate(file_get_contents($endpoint))); $questions = $response->questions[0]; $question_title = $questions->title; $question_body = add_link_to_original($questions->body, $question_id); $answers = $questions->answers; $elgg_question = save_question($question_title, $question_body); foreach ($answers as $answer) { $answer_body = $answer->body; $elgg_answer = save_answer($elgg_question, $answer_body); $comments = $answer->comments; foreach ($comments as $comment) { $comment = $comment->body; save_comment($elgg_answer, $comment); } } echo json_encode(array('success' => true)); die; function add_link_to_original($body, $question_id) { $url = 'http://www.stackoverflow.com/questions/' . $question_id; $message = "<div style='border:2px solid green;'>See the original question at <a href='{$url}'>Stackoverflow</a></div>"; return "{$message}{$body}"; } function save_answer($elgg_question, $answer_body) {
/** * @test * @group sitemap */ public function test_render_gzip() { // Basic URL $url = new Sitemap_URL(); $url->set_loc('http://example.com'); // Add the sitemap url. $instance = new Sitemap(); $instance->add($url); // Enable gzip compression $instance->gzip = TRUE; $instance->compression = 9; // Via render $render = $instance->render(); // Via __toString $tostring = (string) $instance; $this->assertSame(TRUE, NULL !== http_inflate($render)); $this->assertSame(TRUE, NULL !== http_inflate($tostring)); }