WYSIJA_help_http

The MailPoet Newsletters WYSIJA help http class.

Defined (1)

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

/helpers/http.php  
  1. class WYSIJA_help_http extends WYSIJA_object{ 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. /** 
  7. * try three different methods for http request,  
  8. * @param type $url 
  9. * @return type 
  10. */ 
  11. function request($url) { 
  12. // use curl_get first if it is activated 
  13. if(function_exists('curl_init')) { 
  14. $this->opts = array( 
  15. CURLOPT_HEADER => FALSE,  
  16. CURLOPT_RETURNTRANSFER => TRUE 
  17. ); 
  18. $result=$this->curl_get($url); 
  19. return $result['cr']; 
  20. // then try http_get 
  21. }elseif(function_exists('http_get')) { 
  22. return http_parse_message(http_get($url))->body; 
  23.  
  24. // finally we have file_get_contents which is quite often deactivated 
  25. }elseif(ini_get('allow_url_fopen')) { 
  26. return file_get_contents($url); 
  27. }else{ 
  28. $this->error(__('Your server doesn\'t support remote exchanges.', WYSIJA)); 
  29. $this->error(__('Contact your administrator to modify that, it should be configurable.', WYSIJA)); 
  30. $this->error('<strong>CURL library</strong> DISABLED'); 
  31. $this->error('<strong>allow_url_fopen</strong> DISABLED'); 
  32. $this->error('<strong>PECL pecl_http >= 0.1.0</strong> DISABLED'); 
  33. return false; 
  34.  
  35. function wp_request($url) { 
  36. global $wp_version; 
  37.  
  38. $active = get_option( 'active_plugins', array() ); 
  39. $to_send = (object) compact('plugins', 'active'); 
  40.  
  41. $options = array( 
  42. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),  
  43. 'body' => array( 'plugins' => serialize( $to_send ) ),  
  44. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) 
  45. ); 
  46.  
  47. $raw_response = wp_remote_post($url, $options); 
  48.  
  49. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) { 
  50. if(method_exists($raw_response, 'get_error_messages')) { 
  51. $this->error($raw_response->get_error_messages()); 
  52. return false; 
  53.  
  54. return maybe_unserialize( wp_remote_retrieve_body( $raw_response ) ); 
  55.  
  56. function request_timeout($url, $timeout='3') { 
  57. if(function_exists('curl_init')) { 
  58.  
  59. $ch = curl_init( $url ); 
  60. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 0 ); 
  61. curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout ); 
  62. $result = curl_exec( $ch ); 
  63.  
  64. return curl_close( $ch ); 
  65. }elseif(ini_get('allow_url_fopen')) { 
  66. ini_set('default_socket_timeout', (int)$timeout); 
  67. return @file_get_contents($url); 
  68. }elseif(function_exists('http_get')) { 
  69. return @http_get($url, array('timeout'=>(int)$timeout)); 
  70. }else{ 
  71. $this->error(__('Your server doesn\'t support remote exchanges.', WYSIJA)); 
  72. $this->error(__('Contact your administrator to modify that, it should be configurable.', WYSIJA)); 
  73. $this->error('<strong>CURL library</strong> DISABLED'); 
  74. $this->error('<strong>allow_url_fopen</strong> DISABLED'); 
  75. $this->error('<strong>PECL pecl_http >= 0.1.0</strong> DISABLED'); 
  76. return false; 
  77.  
  78. function curl_request($ch, $opt) { 
  79. # assign global options array 
  80. $opts = $this->opts; 
  81. # assign user's options 
  82. foreach($opt as $k=>$v) {$opts[$k] = $v;} 
  83. curl_setopt_array($ch, $opts); 
  84. curl_exec($ch); 
  85. $r['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  86. $r['cr'] = curl_exec($ch); 
  87. $r['ce'] = curl_errno($ch); 
  88. curl_close($ch); 
  89. return $r; 
  90.  
  91. function curl_get($url='', $opt=array()) { 
  92. # create cURL resource 
  93. $ch = curl_init($url); 
  94. return $this->curl_request($ch, $opt); 
  95.  
  96. /** function curl_post($url='', $data=array(), $opt=array()) { 
  97. # set POST options 
  98. $opts[CURLOPT_POST] = TRUE; 
  99. $opts[CURLOPT_POSTFIELDS] = $data; 
  100.   
  101. # create cURL resource 
  102. $ch = curl_init($url); 
  103. return $this->curl_request($ch, $opt); 
  104. }*/