WP_Http_Fopen

HTTP request method uses fopen function to retrieve the url.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/class.wp-http.php  
  1. class WP_Http_Fopen { 
  2. /** 
  3. * Send a HTTP request to a URI using fopen(). 
  4. * This transport does not support sending of headers and body, therefore should not be used in 
  5. * the instances, where there is a body and headers. 
  6. * Notes: Does not support non-blocking mode. Ignores 'redirection' option. 
  7. * @see WP_Http::retrieve For default options descriptions. 
  8. * @access public 
  9. * @since 2.7.0 
  10. * @param string $url URI resource. 
  11. * @param str|array $args Optional. Override the defaults. 
  12. * @return array 'headers', 'body', 'cookies' and 'response' keys. 
  13. */ 
  14. function request($url, $args = array()) { 
  15. $defaults = array( 
  16. 'method' => 'GET', 'timeout' => 5,  
  17. 'redirection' => 5, 'httpversion' => '1.0',  
  18. 'blocking' => true,  
  19. 'headers' => array(), 'body' => null, 'cookies' => array() 
  20. ); 
  21.  
  22. $r = wp_parse_args( $args, $defaults ); 
  23.  
  24. $arrURL = parse_url($url); 
  25.  
  26. if ( false === $arrURL ) 
  27. return new WP_Error('http_request_failed', sprintf(__('Malformed URL: %s'), $url)); 
  28.  
  29. if ( 'http' != $arrURL['scheme'] && 'https' != $arrURL['scheme'] ) 
  30. $url = str_replace($arrURL['scheme'], 'http', $url); 
  31.  
  32. if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) 
  33. $handle = @fopen($url, 'r'); 
  34. else 
  35. $handle = fopen($url, 'r'); 
  36.  
  37. if (! $handle) 
  38. return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $url)); 
  39.  
  40. stream_set_timeout($handle, $r['timeout'] ); 
  41.  
  42. if ( ! $r['blocking'] ) { 
  43. fclose($handle); 
  44. return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  45.  
  46. $strResponse = ''; 
  47. while ( ! feof($handle) ) 
  48. $strResponse .= fread($handle, 4096); 
  49.  
  50. if ( function_exists('stream_get_meta_data') ) { 
  51. $meta = stream_get_meta_data($handle); 
  52. $theHeaders = $meta['wrapper_data']; 
  53. if ( isset( $meta['wrapper_data']['headers'] ) ) 
  54. $theHeaders = $meta['wrapper_data']['headers']; 
  55. } else { 
  56. //$http_response_header is a PHP reserved variable which is set in the current-scope when using the HTTP Wrapper 
  57. //see http://php.oregonstate.edu/manual/en/reserved.variables.httpresponseheader.php 
  58. $theHeaders = $http_response_header; 
  59.  
  60. fclose($handle); 
  61.  
  62. $processedHeaders = WP_Http::processHeaders($theHeaders); 
  63.  
  64. if ( ! empty( $strResponse ) && isset( $processedHeaders['headers']['transfer-encoding'] ) && 'chunked' == $processedHeaders['headers']['transfer-encoding'] ) 
  65. $strResponse = WP_Http::chunkTransferDecode($strResponse); 
  66.  
  67. if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) ) 
  68. $strResponse = WP_Http_Encoding::decompress( $strResponse ); 
  69.  
  70. return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']); 
  71.  
  72. /** 
  73. * Whether this class can be used for retrieving an URL. 
  74. * @since 2.7.0 
  75. * @static 
  76. * @return boolean False means this class can not be used, true means it can. 
  77. */ 
  78. function test($args = array()) { 
  79. if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) ) 
  80. return false; 
  81.  
  82. $use = true; 
  83.  
  84. //PHP does not verify SSL certs, We can only make a request via this transports if SSL Verification is turned off. 
  85. $is_ssl = isset($args['ssl']) && $args['ssl']; 
  86. if ( $is_ssl ) { 
  87. $is_local = isset($args['local']) && $args['local']; 
  88. $ssl_verify = isset($args['sslverify']) && $args['sslverify']; 
  89. if ( $is_local && true != apply_filters('https_local_ssl_verify', true) ) 
  90. $use = true; 
  91. elseif ( !$is_local && true != apply_filters('https_ssl_verify', true) ) 
  92. $use = true; 
  93. elseif ( !$ssl_verify ) 
  94. $use = true; 
  95. else 
  96. $use = false; 
  97.  
  98. return apply_filters('use_fopen_transport', $use, $args);