_fetch_remote_file

Retrieve URL headers and content using WP HTTP Request API.

Description

_fetch_remote_file( (string) $url, (string) $headers = '' ); 

Parameters (2)

0. $url (string)
URL to retrieve
1. $headers — Optional. (string) => ''
Headers to send to the URL.

Usage

  1. if ( !function_exists( '_fetch_remote_file' ) ) { 
  2. require_once ABSPATH . WPINC . '/rss.php'; 
  3.  
  4. // URL to retrieve 
  5. $url = ''; 
  6.  
  7. // Optional. Headers to send to the URL. 
  8. $headers = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _fetch_remote_file($url, $headers); 
  12.  

Defined (1)

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

/wp-includes/rss.php  
  1. function _fetch_remote_file($url, $headers = "" ) { 
  2. $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) ); 
  3. if ( is_wp_error($resp) ) { 
  4. $error = array_shift($resp->errors); 
  5.  
  6. $resp = new stdClass; 
  7. $resp->status = 500; 
  8. $resp->response_code = 500; 
  9. $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility 
  10. return $resp; 
  11.  
  12. // Snoopy returns headers unprocessed. 
  13. // Also note,WP_HTTPlowercases all keys, Snoopy did not. 
  14. $return_headers = array(); 
  15. foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) { 
  16. if ( !is_array($value) ) { 
  17. $return_headers[] = "$key: $value"; 
  18. } else { 
  19. foreach ( $value as $v ) 
  20. $return_headers[] = "$key: $v"; 
  21.  
  22. $response = new stdClass; 
  23. $response->status = wp_remote_retrieve_response_code( $resp ); 
  24. $response->response_code = wp_remote_retrieve_response_code( $resp ); 
  25. $response->headers = $return_headers; 
  26. $response->results = wp_remote_retrieve_body( $resp ); 
  27.  
  28. return $response;