* * @link http://shaman.asiadata.ru * @link http://currency.servisator.ru * */ /** * Data format returned by functions * * Array of elements. Each element is an array with the fields * * country – Country code by ISO * bank – Abbreviation name of the bank * fromvalue – Source currency value as 10 power (... 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10 000 ...) * fromcurrency – Source currency code by ISO * tovalue – Currency exchange rate from 20 to 200 (normalized) * tocurrency – Target currency code by ISO * date - Currency exchange rate date as YYYYMMDD * */ /** * Parser * * Australia (AU) * Reserve Bank of Australia (RBA) * Australian dollar (AUD) * http://www.rba.gov.au/ * * @return * Array of the currencies' exchange rates. */ function currency_rba_get_rates() { $url = 'http://www.rba.gov.au/rss/rss-cb-exchange-rates.xml'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->item)) { foreach ($xml->item as $cur) { $tmp = $cur->children('cb', true); $tmp = $tmp->statistics->exchangeRate; if (isset($tmp->observation->value, $tmp->baseCurrency, $tmp->targetCurrency, $tmp->observationPeriod->period)) { $date = trim((string) $tmp->observationPeriod->period); $result[] = curency_prepare_rate(array( 'country' => 'AU', 'bank' => 'RBA', 'fromvalue' => 1, 'fromcurrency' => (string) $tmp->targetCurrency, 'tovalue' => (double) 1.0/(double) $tmp->observation->value, 'tocurrency' => (string) $tmp->baseCurrency, 'date' => substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2), )); } } } return $result; } /** * Parser * * Armenia (AM) * Central Bank of Armenia (CBA) * Armenian dram (AMD) * http://www.cba.am/ * * @return * Array of the currencies' exchange rates. */ function currency_cba_get_rates() { $url = 'http://www.cba.am/_layouts/rssreader.aspx?rss=280F57B8-763C-4EE4-90E0-8136C13E47DA'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->channel) && isset($xml->channel->item)) { foreach ($xml->channel->item as $cur) { $title = isset($cur->title) ? trim((string) $cur->title) : ''; $pubDdate = isset($cur->pubDate) ? trim((string) $cur->pubDate) : ''; $fromvalue = $fromcurrency = $tovalue = $date = NULL; if(preg_match('/^([A-Z]{3})[- ]*([0-9]*)[- ]*([\.0-9]*)$/', $title, $regs)){ $fromcurrency = (string) $regs[1]; $fromvalue = (int) $regs[2]; $tovalue = (double) $regs[3]; } if(preg_match('/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4}).*$/', $pubDdate, $regs)){ $date = $regs[3] . ($regs[1] > 10 ? $regs[1] : '0'. $regs[1]) . ($regs[2] > 10 ? $regs[2] : '0'. $regs[2]); } if (!(is_null($fromvalue) || is_null($fromcurrency) || is_null($tovalue)) && ($tovalue > 0)) { $result[] = curency_prepare_rate(array( 'country'=>'AM', 'bank'=>'CBA', 'fromvalue'=>$fromvalue, 'fromcurrency'=>$fromcurrency, 'tovalue'=>$tovalue, 'tocurrency'=>'AMD', 'date'=>$date )); } } } return $result; } /** * Parser * * Republic of Belarus (BY) * National Bank of the Republic of Belarus (NBRB) * Belarusian ruble (BYR) * http://www.nbrb.by/ * * @return * Array of the currencies' exchange rates. */ function currency_nbrb_get_rates() { $url = 'http://www.nbrb.by/statistics/Rates/RatesPrint.asp'; $result = array(); if ( ($datafetch = currency_http_request($url)) && ($html = currency_convert_to_utf8($datafetch->data, 'windows-1251')) && // eregi("на[[:space:]]+([0-9][0-9]).([0-9][0-9]).([0-9][0-9][0-9][0-9])", $html, $tmp) preg_match('/на[[:space:]]+([0-9][0-9]).([0-9][0-9]).([0-9][0-9][0-9][0-9])/i', $html, $tmp) ) { $date = $tmp[3].$tmp[2].$tmp[1]; $tables = preg_split ('/]*>/i', $html); $table = isSet ($tables[1]) ? $tables[1] : ""; $trs = preg_split('/]*>/i', $table); $lines = array(); foreach($trs as $i=>$l) { $c = preg_split('/]*>/i', $l); $lines[] = currency_clear_array($c); } foreach ($lines as $line) { if ($line[0] && $line[1] && ($line[2] > 0)) { $result[] = curency_prepare_rate(array( 'country' => 'BY', 'bank' => 'NBRB', 'fromvalue' => (int) $line[1], 'fromcurrency' => $line[0], 'tovalue' => doubleval(0.0 + $line[2]), 'tocurrency' => 'BYR', 'date' => $date, )); } } return $result; } } /** * Parser * * Georgia (GE) * National Bank of Georgia (NBG) * Georgian lari (GEL) * http://www.nbg.ge/ * * @return * Array of the currencies' exchange rates. */ function currency_nbg_get_rates() { $url = 'http://www.nbg.ge/rss.php'; $result = array(); if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) { $tables = preg_split('/]*>/i', $html); $table = isSet ($tables[1]) ? $tables[1] : ""; $trs = preg_split('/]*>/i', $table); $lines = array(); foreach($trs as $i=>$l) { $l = preg_replace('/<\/tr>.*/', '', $l); $c = preg_split('/]*>/i', $l); $lines[] = currency_clear_array($c); } $dtable = isSet ($tables[0]) ? $tables[0] : ""; $date = preg_match('/Currency Rates ([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])<\/title>/i', $dtable, $regs) ? $regs[1] . $regs[2] . $regs[3] : date('Ymd'); foreach ($lines as $line) { if ($line[0] && $line[1] && $line[2]) { $result[] = curency_prepare_rate(array( 'country' => 'GE', 'bank' => 'NBG', 'fromvalue' => (int) $line[1], 'fromcurrency' => $line[0], 'tovalue' => (double) (0.0 + $line[2]), 'tocurrency' => 'GEL', 'date' => $date, )); } } } return $result; } /** * Parser * * Europe (EU) * European Central Bank (ECB) * Euro (EUR) * http://www.ecb.europa.eu/ * * @return * Array of the currencies' exchange rates. */ function currency_ecb_get_rates() { $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->Cube->Cube)) { $cube = $xml->Cube->Cube; $date = date('Ymd'); foreach($cube->attributes() as $n=>$v) if($n=='time') $date = implode('', explode('-', $v)); foreach ($cube->Cube as $cur) { $fromvalue = $fromcurrency = NULL; foreach($cur->attributes() as $n=>$v) if($n=='currency') { $fromcurrency = (string) $v; } elseif ($n=='rate') { $fromvalue = $v; } if (!(is_null($fromvalue) || is_null($fromcurrency))) { $result[] = curency_prepare_rate(array( 'country'=>'EU', 'bank'=>'ECB', 'fromvalue'=>1, 'fromcurrency'=>$fromcurrency, 'tovalue'=>(double) 1.0/(double) $fromvalue, 'tocurrency'=>'EUR', 'date'=>$date )); } } } return $result; } /** * Parser * * Israil (IL) * Bank of Israil (BOI) * Israeli new sheqel (ILS) * http://www.bankisrael.gov.il/ * * @return * Array of the currencies' exchange rates. */ function currency_boi_get_rates() { $url = 'http://www.bankisrael.gov.il/currency.xml'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->CURRENCY) && isset($xml->LAST_UPDATE) ) { $date = implode('', explode('-', $xml->LAST_UPDATE)); foreach ($xml->CURRENCY as $cur) { $fromvalue = isset($cur->UNIT) ? (int) $cur->UNIT : NULL; $fromcurrency = isset($cur->CURRENCYCODE) ? (string) $cur->CURRENCYCODE : NULL; $tovalue = isset($cur->RATE) ? (double) $cur->RATE : NULL; if (!(is_null($fromvalue) || is_null($fromcurrency) || is_null($tovalue))) { $result[] = curency_prepare_rate(array( 'country'=>'IL', 'bank'=>'BOI', 'fromvalue'=>$fromvalue, 'fromcurrency'=>$fromcurrency, 'tovalue'=>$tovalue, 'tocurrency'=>'ILS', 'date'=>$date )); } } } return $result; } /** * Parser * * Kazakhstan (KZ) * National Bank of Kazakhstan (NBK) * Kazakhstani tenge (KZT) * http://www.nationalbank.kz/ * * @return * Array of the currencies' exchange rates. */ function currency_nbk_get_rates() { $url = 'http://www.nationalbank.kz/rss/rates_all.xml'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->channel->item)) { foreach ($xml->channel->item as $cur) { $fromvalue = isset($cur->quant) ? (int) $cur->quant : NULL; $fromcurrency = isset($cur->title) ? (string) $cur->title : NULL; $tovalue = isset($cur->description) ? (double) $cur->description : NULL; $date = '20'. implode('', array_reverse(explode('.', (string) $cur->pubDate))); if (!(is_null($fromvalue) || is_null($fromcurrency) || is_null($tovalue) || empty($date))) { $result[] = curency_prepare_rate(array( 'country'=>'KZ', 'bank'=>'NBK', 'fromvalue'=>$fromvalue, 'fromcurrency'=>$fromcurrency, 'tovalue'=>$tovalue, 'tocurrency'=>'KZT', 'date'=>$date )); } } } return $result; } /** * Parser * * Canada (CA) * Banque du Canada () * Canadian dollar (CAD) * http://www.bankofcanada.ca/ * * @return * Array of the currencies' exchange rates. */ function currency_bdc_get_rates() { $url = 'http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_all.xml'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && isset($xml->item)) { foreach ($xml->item as $cur) { $tmp = $cur->children('cb', true); $tmp = $tmp->statistics->exchangeRate; if (isset($tmp->value, $tmp->baseCurrency, $tmp->targetCurrency, $tmp->observationPeriod)) { $date = trim((string) $tmp->observationPeriod); $result[] = curency_prepare_rate(array( 'country' => 'CA', 'bank' => 'BDC', 'fromvalue' => 1, 'fromcurrency' => (string) $tmp->targetCurrency, 'tovalue' => (double) 1.0/(double) $tmp->value, 'tocurrency' => (string) $tmp->baseCurrency, 'date' => substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2), )); } } } return $result; } /** * Parser * * China (CN) * Bank of China (BOC) * Chinese yuan (CNY) * http://www.boc.cn/ * * @return * Array of the currencies' exchange rates. */ function currency_boc_get_rates() { $url = 'http://www.boc.cn/sourcedb/whpj/enindex.html'; $result = array(); if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) { $tables = preg_split('/<table[^>]*>/i', $html); $table = isSet ($tables[8]) ? $tables[8] : ""; $trs = preg_split('/<tr[^>]*>/i', $table); $lines = array(); foreach($trs as $i=>$l) { $l = preg_replace('/<\/tr>.*/i', '', $l); $c = preg_split('/<td[^>]*>/i', $l); $lines[] = currency_clear_array($c); } $date = date('Ymd'); foreach ($lines as $line) { if ($line[0] && $line[1] && $line[3] && preg_match('/([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])/i', $line[6], $regs)) { $date = $regs[1] . $regs[2] . $regs[3]; $result[] = curency_prepare_rate(array( 'country'=>'CN', 'bank'=>'BOC', 'fromvalue'=>100, 'fromcurrency'=>$line[0], 'tovalue'=>(doubleval ($line[1]) + doubleval ($line[3]))/2.0, 'tocurrency'=>'CNY', 'date'=>$date, )); } } } return $result; } /** * Parser * * Mongolia (CN) * Bank of Mongolia (BOM) * Mongolian tugrik (MNT) * http://www.mongolbank.mn/ * * @return * Array of the currencies' exchange rates. */ function currency_bom_get_rates() { $url = 'http://www.mongolbank.mn/eng/dblistofficialdailyrate.aspx'; $result = array(); if (($datafetch = currency_http_request($url)) && ($html = $datafetch->data)) { $tables = preg_split('/<table[^>]*>/is', $html); $trs = array_slice($tables, 12, 37); $lines = array(); foreach($trs as $i=>$l) { $l = preg_replace('/<\/span>.*/is', '', $l); $c = spliti ('<td[^>]*>', $l); $lines[] = currency_clear_array($c); } $date = date('Ymd'); $dtable = isSet ($tables[11]) ? $tables[11] : ""; $months = array('Jan'=>'01','Feb'=>'02','Mar'=>'03','Apr'=>'04','May'=>'05','Jun'=>'06','Jul'=>'07','Aug'=>'08','Sep'=>'09','Oct'=>'10','Nov'=>'11','Dec'=>'12'); if(preg_match('/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{1,2}), ([0-9][0-9][0-9][0-9])/', $dtable, $regs)) { $date = $regs[3].$months[$regs[1]].$regs[2]; } foreach ($lines as $line) { $line[2] = str_replace(array(' ', ','), '', $line[2]); if ($line[1] && ($line[2] > 0)) { $result[] = curency_prepare_rate(array( 'country'=>'MN', 'bank'=>'BOM', 'fromvalue'=>1, 'fromcurrency'=>$line[1], 'tovalue'=>doubleval($line[2]), 'tocurrency'=>'MNT', 'date'=>$date, )); } } } return $result; } /** * Parser * * Russia (RU) * Central Bank of Russia (CBR) * Russian rouble (RUB) * http://www.cbr.ru/ * * @return * Array of the currencies' exchange rates. */ function currency_cbr_get_rates() { $url = 'http://www.cbr.ru/scripts/XML_daily.asp'; $result = array(); if (($datafetch = currency_http_request($url)) && ($xml = simplexml_load_string($datafetch->data)) && ($xml->getName()=='ValCurs')) { $date = date('Ymd'); foreach($xml->attributes() as $n=>$v) if($n=='Date') $date = implode('', array_reverse(explode('.', $v))); foreach ($xml->Valute as $cur) if ($cur->getName()=='Valute' && isset($cur->CharCode, $cur->Nominal, $cur->Value)) $result[] = curency_prepare_rate(array( 'country' => 'RU', 'bank' => 'CBR', 'fromvalue' => str_replace (',', '.', (string) $cur->Nominal), 'fromcurrency' => (string) $cur->CharCode, 'tovalue' => str_replace(',', '.', (string) $cur->Value), 'tocurrency' => 'RUB', 'date' => $date )); } return $result; } /** * Parser * * Ukraine (UA) * National Bank of Ukraine (NBU) * Ukrainian hryvnia (UAH) * http://www.bank.gov.ua/ * * @return * Array of the currencies' exchange rates. */ function currency_nbu_get_rates() { function currency_nbu_html_parse($html) { $result = array(); $tables = preg_split('/<table[^>]*>/i', $html); $table = isSet ($tables[6]) ? $tables[6] : ""; $trs = preg_split('/<tr[^>]*>/i', $table); unset($trs[0], $trs[1]); $lines = array(); foreach($trs as $i=>$l) { $l = preg_replace('/<\/tr>.*/i', '', $l); $c = preg_split('/<td[^>]*>/i', $l); $lines[] = currency_clear_array($c); } $dtable = isSet ($tables[5]) ? $tables[5] : ""; $date = preg_match('/([0-9][0-9]).([0-9][0-9]).([0-9][0-9][0-9][0-9])/i', $dtable, $regs) ? $regs[3] . $regs[2] . $regs[1] : date('Ymd'); foreach ($lines as $line) { if ($line[1] && $line[2] && $line[4]) { $result[] = curency_prepare_rate(array( 'country'=>'UA', 'bank'=>'NBU', 'fromvalue'=>$line[2], 'fromcurrency'=>$line[1], 'tovalue'=>doubleval($line[4]), 'tocurrency'=>'UAH', 'date'=>$date, )); } } return $result; } $d_url = 'http://bank.gov.ua/control/en/curmetal/detail/currency?period=daily'; $m_url = 'http://bank.gov.ua/control/en/curmetal/detail/currency?period=monthly'; $result = array(); if (($datafetch = currency_http_request($d_url)) && ($html = $datafetch->data)) { $result = currency_nbu_html_parse($html); } if (($datafetch = currency_http_request($m_url)) && ($html = $datafetch->data)) { $result = array_merge($result, currency_nbu_html_parse($html)); } return $result; } /** * Common functions for the project. * */ /** * Prepare rates to readable view * * Prepare currencies' exchange rates to readable view. * * @param $rate * Array with a currency's rate. * @param $low_limit * The lower limit of the normalization. * @param $precision * Rounding precision of the rate. * @return * Normalizing and rounding rate. */ function curency_prepare_rate($rate, $low_limit = 20, $precision = 2) { $low_limit = (int) $low_limit; $high_limit = $low_limit*10; $precision = (int) $precision; $tovalue = $rate['tovalue']; $fromvalue = $rate['fromvalue']; if($tovalue < $low_limit) { while ($tovalue < $low_limit) { $fromvalue = $fromvalue*10; $tovalue = $tovalue*10.; } } else if ($tovalue>=$high_limit) { while ($tovalue>=$high_limit) { $fromvalue = $fromvalue/10; $tovalue = $tovalue/10.; } } $rate['tovalue'] = round($tovalue, $precision); $rate['fromvalue'] = $fromvalue; return $rate; } /** * Clear array of the strings from HTML-code * * @param $arr * The array of the strings to be cleared. * @return * Cleared array of the strings. */ function currency_clear_array($arr) { $res = array(); $s = array('</table>', '<br>', '</body>', '</html>', ' ', ' ', "\xC2"."\xA0"); $r = array( '', ' ', '', '', '', '', ''); array_shift($arr); foreach ($arr as $el) { $el = trim (strip_tags (str_replace( $s, $r, $el))); /*if (!empty ($el)) */$res[] = $el; } return $res; } /** * Convert data to UTF-8 * * Requires the iconv, GNU recode or mbstring PHP extension. * * @param $data * The data to be converted. * @param $encoding * The encoding that the data is in * @return * Converted data or FALSE. */ function currency_convert_to_utf8($data, $encoding) { if (function_exists('iconv')) { $out = @iconv($encoding, 'utf-8', $data); } else if (function_exists('mb_convert_encoding')) { $out = @mb_convert_encoding($encoding, $data, 'utf-8'); } else { return FALSE; } return $out; } /** * Perform an HTTP request. * * This is a flexible and powerful HTTP client implementation. Correctly handles * GET, POST, PUT or any other HTTP requests. Handles redirects. * * @param $url * A string containing a fully qualified URI. * @param $headers * An array containing an HTTP header => value pair. * @param $method * A string defining the HTTP request to use. * @param $data * A string containing data to include in the request. * @param $retry * An integer representing how many times to retry the request in case of a * redirect. * @return * An object containing the HTTP request headers, response code, protocol, * status message, headers, data and redirect status. */ function currency_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) { $result = new stdClass(); // Parse the URL and make sure we can handle the schema. $uri = parse_url($url); if ($uri == FALSE) { $result->error = 'unable to parse URL'; $result->code = -1001; return $result; } if (!isset($uri['scheme'])) { $result->error = 'missing schema'; $result->code = -1002; return $result; } switch ($uri['scheme']) { case 'http': case 'feed': $port = isset($uri['port']) ? $uri['port'] : 80; $host = $uri['host'] . ($port != 80 ? ':'. $port : ''); $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15); break; case 'https': // Note: Only works for PHP 4.3 compiled with OpenSSL. $port = isset($uri['port']) ? $uri['port'] : 443; $host = $uri['host'] . ($port != 443 ? ':'. $port : ''); $fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, 20); break; default: $result->error = 'invalid schema '. $uri['scheme']; $result->code = -1003; return $result; } // Make sure the socket opened properly. if (!$fp) { // When a network error occurs, we use a negative number so it does not // clash with the HTTP status codes. $result->code = -$errno; $result->error = trim($errstr); return $result; } // Construct the path to act on. $path = isset($uri['path']) ? $uri['path'] : '/'; if (isset($uri['query'])) { $path .= '?'. $uri['query']; } // Create HTTP request. $defaults = array( // RFC 2616: "non-standard ports MUST, default ports MAY be included". // We don't add the port to prevent from breaking rewrite rules checking the // host that do not take into account the port number. 'Host' => "Host: $host", 'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)', ); // Only add Content-Length if we actually have any content or if it is a POST // or PUT request. Some non-standard servers get confused by Content-Length in // at least HEAD/GET requests, and Squid always requires Content-Length in // POST/PUT requests. $content_length = strlen($data); if ($content_length > 0 || $method == 'POST' || $method == 'PUT') { $defaults['Content-Length'] = 'Content-Length: '. $content_length; } // If the server url has a user then attempt to use basic authentication if (isset($uri['user'])) { $defaults['Authorization'] = 'Authorization: Basic '. base64_encode($uri['user'] . (!empty($uri['pass']) ? ":". $uri['pass'] : '')); } foreach ($headers as $header => $value) { $defaults[$header] = $header .': '. $value; } $request = $method .' '. $path ." HTTP/1.0\r\n"; $request .= implode("\r\n", $defaults); $request .= "\r\n\r\n"; $request .= $data; $result->request = $request; fwrite($fp, $request); // Fetch response. $response = ''; while (!feof($fp) && $chunk = fread($fp, 1024)) { $response .= $chunk; } fclose($fp); // Parse response. list($split, $result->data) = explode("\r\n\r\n", $response, 2); $split = preg_split("/\r\n|\n|\r/", $split); list($protocol, $code, $status_message) = explode(' ', trim(array_shift($split)), 3); $result->protocol = $protocol; $result->status_message = $status_message; $result->headers = array(); // Parse headers. while ($line = trim(array_shift($split))) { list($header, $value) = explode(':', $line, 2); if (isset($result->headers[$header]) && $header == 'Set-Cookie') { // RFC 2109: the Set-Cookie response header comprises the token Set- // Cookie:, followed by a comma-separated list of one or more cookies. $result->headers[$header] .= ','. trim($value); } else { $result->headers[$header] = trim($value); } } $responses = array( 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' ); // RFC 2616 states that all unknown HTTP codes must be treated the same as the // base code in their class. if (!isset($responses[$code])) { $code = floor($code / 100) * 100; } switch ($code) { case 200: // OK case 304: // Not modified break; case 301: // Moved permanently case 302: // Moved temporarily case 307: // Moved temporarily $location = $result->headers['Location']; if ($retry) { $result = currency_http_request($result->headers['Location'], $headers, $method, $data, --$retry); $result->redirect_code = $result->code; } $result->redirect_url = $location; break; default: $result->error = $status_message; } $result->code = $code; return $result; }