WP_Http_Curl

Core class used to integrate Curl as an HTTP transport.

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_Curl { 
  2.  
  3. /** 
  4. * Send a HTTP request to a URI using cURL extension. 
  5. * @access public 
  6. * @since 2.7.0 
  7. * @param string $url 
  8. * @param str|array $args Optional. Override the defaults. 
  9. * @return array 'headers', 'body', 'cookies' and 'response' keys. 
  10. */ 
  11. function request($url, $args = array()) { 
  12. $defaults = array( 
  13. 'method' => 'GET', 'timeout' => 5,  
  14. 'redirection' => 5, 'httpversion' => '1.0',  
  15. 'blocking' => true,  
  16. 'headers' => array(), 'body' => null, 'cookies' => array() 
  17. ); 
  18.  
  19. $r = wp_parse_args( $args, $defaults ); 
  20.  
  21. if ( isset($r['headers']['User-Agent']) ) { 
  22. $r['user-agent'] = $r['headers']['User-Agent']; 
  23. unset($r['headers']['User-Agent']); 
  24. } else if( isset($r['headers']['user-agent']) ) { 
  25. $r['user-agent'] = $r['headers']['user-agent']; 
  26. unset($r['headers']['user-agent']); 
  27.  
  28. // Construct Cookie: header if any cookies are set. 
  29. WP_Http::buildCookieHeader( $r ); 
  30.  
  31. // cURL extension will sometimes fail when the timeout is less than 1 as it may round down 
  32. // to 0, which gives it unlimited timeout. 
  33. if ( $r['timeout'] > 0 && $r['timeout'] < 1 ) 
  34. $r['timeout'] = 1; 
  35.  
  36. $handle = curl_init(); 
  37.  
  38. // cURL offers really easy proxy support. 
  39. $proxy = new WP_HTTP_Proxy(); 
  40.  
  41. if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { 
  42.  
  43. $isPHP5 = version_compare(PHP_VERSION, '5.0.0', '>='); 
  44.  
  45. if ( $isPHP5 ) { 
  46. curl_setopt( $handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); 
  47. curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() ); 
  48. curl_setopt( $handle, CURLOPT_PROXYPORT, $proxy->port() ); 
  49. } else { 
  50. curl_setopt( $handle, CURLOPT_PROXY, $proxy->host() .':'. $proxy->port() ); 
  51.  
  52. if ( $proxy->use_authentication() ) { 
  53. if ( $isPHP5 ) 
  54. curl_setopt( $handle, CURLOPT_PROXYAUTH, CURLAUTH_BASIC ); 
  55.  
  56. curl_setopt( $handle, CURLOPT_PROXYUSERPWD, $proxy->authentication() ); 
  57.  
  58. $is_local = isset($args['local']) && $args['local']; 
  59. $ssl_verify = isset($args['sslverify']) && $args['sslverify']; 
  60. if ( $is_local ) 
  61. $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify); 
  62. elseif ( ! $is_local ) 
  63. $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify); 
  64.  
  65. curl_setopt( $handle, CURLOPT_URL, $url); 
  66. curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); 
  67. curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify ); 
  68. curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify ); 
  69. curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] ); 
  70. curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] ); 
  71. curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] ); 
  72. curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] ); 
  73.  
  74. switch ( $r['method'] ) { 
  75. case 'HEAD': 
  76. curl_setopt( $handle, CURLOPT_NOBODY, true ); 
  77. break; 
  78. case 'POST': 
  79. curl_setopt( $handle, CURLOPT_POST, true ); 
  80. curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] ); 
  81. break; 
  82.  
  83. if ( true === $r['blocking'] ) 
  84. curl_setopt( $handle, CURLOPT_HEADER, true ); 
  85. else 
  86. curl_setopt( $handle, CURLOPT_HEADER, false ); 
  87.  
  88. // The option doesn't work with safe mode or when open_basedir is set. 
  89. if ( !ini_get('safe_mode') && !ini_get('open_basedir') ) 
  90. curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true ); 
  91.  
  92. if ( !empty( $r['headers'] ) ) { 
  93. // cURL expects full header strings in each element 
  94. $headers = array(); 
  95. foreach ( $r['headers'] as $name => $value ) { 
  96. $headers[] = "{$name}: $value"; 
  97. curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers ); 
  98.  
  99. if ( $r['httpversion'] == '1.0' ) 
  100. curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 ); 
  101. else 
  102. curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); 
  103.  
  104. // Cookies are not handled by the HTTP API currently. Allow for plugin authors to handle it 
  105. // themselves... Although, it is somewhat pointless without some reference. 
  106. do_action_ref_array( 'http_api_curl', array(&$handle) ); 
  107.  
  108. // We don't need to return the body, so don't. Just execute request and return. 
  109. if ( ! $r['blocking'] ) { 
  110. curl_exec( $handle ); 
  111. curl_close( $handle ); 
  112. return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 
  113.  
  114. $theResponse = curl_exec( $handle ); 
  115.  
  116. if ( !empty($theResponse) ) { 
  117. $parts = explode("\r\n\r\n", $theResponse); 
  118.  
  119. $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); 
  120. $theHeaders = trim( substr($theResponse, 0, $headerLength) ); 
  121. $theBody = substr( $theResponse, $headerLength ); 
  122. if ( false !== strrpos($theHeaders, "\r\n\r\n") ) { 
  123. $headerParts = explode("\r\n\r\n", $theHeaders); 
  124. $theHeaders = $headerParts[ count($headerParts) -1 ]; 
  125. $theHeaders = WP_Http::processHeaders($theHeaders); 
  126. } else { 
  127. if ( $curl_error = curl_error($handle) ) 
  128. return new WP_Error('http_request_failed', $curl_error); 
  129. if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) ) 
  130. return new WP_Error('http_request_failed', __('Too many redirects.')); 
  131.  
  132. $theHeaders = array( 'headers' => array(), 'cookies' => array() ); 
  133. $theBody = ''; 
  134.  
  135. $response = array(); 
  136. $response['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE ); 
  137. $response['message'] = get_status_header_desc($response['code']); 
  138.  
  139. curl_close( $handle ); 
  140.  
  141. if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) ) 
  142. $theBody = WP_Http_Encoding::decompress( $theBody ); 
  143.  
  144. return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']); 
  145.  
  146. /** 
  147. * Whether this class can be used for retrieving an URL. 
  148. * @static 
  149. * @since 2.7.0 
  150. * @return boolean False means this class can not be used, true means it can. 
  151. */ 
  152. function test($args = array()) { 
  153. if ( function_exists('curl_init') && function_exists('curl_exec') ) 
  154. return apply_filters('use_curl_transport', true, $args); 
  155.  
  156. return false;