composer example

composer example

<?php
include __DIR__."/vendor/autoload.php";

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('usdToZar');
$log->pushHandler(new StreamHandler('/var/www/html/test/log', Logger::DEBUG));

// add records to the log
$log->info("About to get exchange rate");

$client = new \GuzzleHttp\Client();

try {
    $res = $client->request('GET', 'http://api.softsmart.co.za/exchange/');
}
catch (Exception $e) {
    print "An error occurred and has been logged. Please check the log file for info!";
    $log->error("http request error resulted in an error", array("error"=>$e));
    exit();
}

$log->info("http result: ".$res->getStatusCode());

if ($res->getStatusCode() != 200) {
    $log->error("http request failed", array("result"=>$res->getStatusCode()));
    exit();
}

if ( ! strstr($res->getHeaderLine('content-type'), "application/json")) {
    $log->error("Invalid http response content type", array("content-type"=>$res->getHeaderLine('content-type')));
    exit();
}

$exchange = json_decode($res->getBody());

print "1 usd = ".$exchange->rate." zar";

Comments are closed.