function http_request($url, $fields = array(), $method = 'POST') { if (!in_array($method, array('POST', 'GET'))) { $method = 'POST'; } if (!isset($fields['key'])) { $fields['key'] = $this->api; } //some distribs change arg sep to & by default $sep_changed = false; if (ini_get("arg_separator.output") != "&") { $sep_changed = true; $orig_sep = ini_get("arg_separator.output"); ini_set("arg_separator.output", "&"); } $fields = is_array($fields) ? http_build_query($fields) : $fields; if ($sep_changed) { ini_set("arg_separator.output", $orig_sep); } $useragent = wpMandrill::getUserAgent(); if (function_exists('curl_init') && function_exists('curl_exec')) { if (!ini_get('safe_mode')) { set_time_limit(2 * 60); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, $method == 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // @Bruno Braga: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Thanks for the hack! curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2 * 60 * 1000); $response = curl_exec($ch); $info = curl_getinfo($ch); $error = curl_error($ch); curl_close($ch); } elseif (function_exists('fsockopen')) { $parsed_url = parse_url($url); $host = $parsed_url['host']; if (isset($parsed_url['path'])) { $path = $parsed_url['path']; } else { $path = '/'; } $params = ''; if (isset($parsed_url['query'])) { $params = $parsed_url['query'] . '&' . $fields; } elseif (trim($fields) != '') { $params = $fields; } if (isset($parsed_url['port'])) { $port = $parsed_url['port']; } else { $port = $parsed_url['scheme'] == 'https' ? 443 : 80; } $response = false; $errno = ''; $errstr = ''; ob_start(); $fp = fsockopen('ssl://' . $host, $port, $errno, $errstr, 5); if ($fp !== false) { stream_set_timeout($fp, 30); $payload = "{$method} {$path} HTTP/1.0\r\n" . "Host: {$host}\r\n" . "Connection: close\r\n" . "User-Agent: {$useragent}\r\n" . "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: " . strlen($params) . "\r\n" . "Connection: close\r\n\r\n" . $params; fwrite($fp, $payload); stream_set_timeout($fp, 30); $info = stream_get_meta_data($fp); while (!feof($fp) && !$info["timed_out"]) { $response .= fread($fp, 4096); $info = stream_get_meta_data($fp); } fclose($fp); ob_end_clean(); list($headers, $response) = explode("\r\n\r\n", $response, 2); if (ini_get("magic_quotes_runtime")) { $response = stripslashes($response); } $info = array('http_code' => 200); } else { ob_end_clean(); $info = array('http_code' => 500); throw new Exception($errstr, $errno); } $error = ''; } else { throw new Mandrill_Exception("No valid HTTP transport found", -99); } return array('header' => $info, 'body' => $response, 'error' => $error); }