note 72944 rejected from function.socket-create by nicobn
| From: | nicobn@php.net | Date: | Tue, 03 Jul 2007 19:47:16 +0000 |
| Subject: | note 72944 rejected from function.socket-create by nicobn | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128190@lists.php.net to get a copy of this message | ||
Note Submitter: gancheff at gmail dot com
----
here's a way to get the currency:
example getCurrency("BGN", "USD");
function getCurrency($from, $to, $cache = true) {
if ($cache && $_SESSION['currency'][$from][$to]['value'] &&
(time() - $_SESSION['currency'][$from][$to]['time']) < 3600)
return $_SESSION['currency'][$from][$to]['value'];
$host = "www.xe.com";
$port = "80";
$data = "Amount=1&From=" . strtoupper($from) . "&To=" .
strtoupper($to);
$header =
"POST /ucc/convert.cgi HTTP/1.1\n" .
"Host: $host:$port\n" .
"Content-Type: application/x-www-form-urlencoded\n" .
"Content-Length: " . strlen($data) . "\n\n" .
$data . "\n";
$s = socket_create(AF_INET, SOCK_STREAM, 0);
$z = socket_connect($s, gethostbyname($host), $port);
socket_write ($s, $header, strlen($header));
while (true) {
$c = @socket_read($s, 1);
if ($c == "\n") {
if (preg_match("/$from = (.+) $to/i", $line, $matches) ||
preg_match("/<\/html>/i", $line)) break;
$line = "";
} else $line .= $c;
}
socket_close($s);
$_SESSION['currency'][$from][$to]['time'] = time();
$_SESSION['currency'][$from][$to]['value'] = str_pad(round($matches[1], 5),
7, "0");
return ($matches[1]) ? str_pad(round($matches[1], 5), 7, "0") : "0.00000";
}