-function _fetch_remote_file ($url, $headers = "" ) {
- // Snoopy is an HTTP client in PHP
- $client = new Snoopy();
- $client->agent = MAGPIE_USER_AGENT;
- $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
- $client->use_gzip = MAGPIE_USE_GZIP;
- if (is_array($headers) ) {
- $client->rawheaders = $headers;
+/**
+ * Retrieve URL headers and content using WP HTTP Request API.
+ *
+ * @since 1.5.0
+ * @package External
+ * @subpackage MagpieRSS
+ *
+ * @param string $url URL to retrieve
+ * @param array $headers Optional. Headers to send to the URL.
+ * @return Snoopy style response
+ */
+function _fetch_remote_file($url, $headers = "" ) {
+ $resp = wp_remote_request($url, array('headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT));
+ if ( is_wp_error($resp) ) {
+ $error = array_shift($resp->errors);
+
+ $resp = new stdClass;
+ $resp->status = 500;
+ $resp->response_code = 500;
+ $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility
+ return $resp;
+ }
+
+ // Snoopy returns headers unprocessed.
+ // Also note, WP_HTTP lowercases all keys, Snoopy did not.
+ $return_headers = array();
+ foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
+ if ( !is_array($value) ) {
+ $return_headers[] = "$key: $value";
+ } else {
+ foreach ( $value as $v )
+ $return_headers[] = "$key: $v";
+ }