wp_get_http

Perform a HTTP HEAD or GET request.

Description

(bool|string) wp_get_http( (string) $url, (constant) $file_path = false, (int) $red = 1 ); 

If $file_path is a writable filename, this will do a GET request and write the file to that path.

Returns (bool|string)

False on failure and string of headers if HEAD request.

Parameters (3)

0. $url (string)
URL to fetch.
1. $file_path — Optional. (constant) => false
File path to write request to. Default false.
2. $red — Optional. (int) => 1
The number of Redirects followed, Upon 5 being hit, returns false. Default 1.

Usage

  1. if ( !function_exists( 'wp_get_http' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // URL to fetch. 
  5. $url = ''; 
  6.  
  7. // Optional. File path to write request to. Default false. 
  8. $file_path = false; 
  9.  
  10. // Optional. The number of Redirects followed, Upon 5 being hit, 
  11. // returns false. Default 1. 
  12. $red = 1; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_get_http($url, $file_path, $red); 
  16.  

Defined (1)

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

/wp-includes/deprecated.php  
  1. function wp_get_http( $url, $file_path = false, $red = 1 ) { 
  2. _deprecated_function( __FUNCTION__, '4.4.0', 'WP_Http' ); 
  3.  
  4. @set_time_limit( 60 ); 
  5.  
  6. if ( $red > 5 ) 
  7. return false; 
  8.  
  9. $options = array(); 
  10. $options['redirection'] = 5; 
  11.  
  12. if ( false == $file_path ) 
  13. $options['method'] = 'HEAD'; 
  14. else 
  15. $options['method'] = 'GET'; 
  16.  
  17. $response = wp_safe_remote_request( $url, $options ); 
  18.  
  19. if ( is_wp_error( $response ) ) 
  20. return false; 
  21.  
  22. $headers = wp_remote_retrieve_headers( $response ); 
  23. $headers['response'] = wp_remote_retrieve_response_code( $response ); 
  24.  
  25. //WP_HTTPno longer follows redirects for HEAD requests. 
  26. if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) { 
  27. return wp_get_http( $headers['location'], $file_path, ++$red ); 
  28.  
  29. if ( false == $file_path ) 
  30. return $headers; 
  31.  
  32. // GET request - write it to the supplied filename 
  33. $out_fp = fopen($file_path, 'w'); 
  34. if ( !$out_fp ) 
  35. return $headers; 
  36.  
  37. fwrite( $out_fp, wp_remote_retrieve_body( $response ) ); 
  38. fclose($out_fp); 
  39. clearstatcache(); 
  40.  
  41. return $headers;