* Add initial support for PHP7 * Update PHP env with now an PSR3 logger (Monolog). Now you can also create a handler function with PSR7 Http message and PSR3 logger in parameter. * Add examples for PHP7 environement * For development you can create a function in this path /app/userfuncdev.php it will be loaded * Switch to alpine base image * handler now receive an array with request, response and logger object
48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
use \Psr\Http\Message\ResponseInterface;
|
|
use \Psr\Http\Message\ServerRequestInterface;
|
|
use \Psr\Log\LoggerInterface;
|
|
|
|
function sendError($response,$message){
|
|
$response = $response->withStatus(500);
|
|
$response->getBody()->write($message);
|
|
return $response;
|
|
}
|
|
|
|
function handler($context){
|
|
/** @var ResponseInterface $response */
|
|
$response = $context["response"];
|
|
/** @var ServerRequestInterface $request */
|
|
$request = $context["request"];
|
|
/** @var LoggerInterface $logger */
|
|
$logger = $context["logger"];
|
|
|
|
$logger->debug("Request : ",$request->getParsedBody());
|
|
if($request->getMethod() != "POST")
|
|
return sendError($response,"You must use POST method");
|
|
|
|
$body = $request->getParsedBody();
|
|
if(!isset($body["currency"]))
|
|
return sendError($response,"'currency' is not present in the POST request");
|
|
|
|
$allowedCurrency = ["ltc","btc"];
|
|
if(!in_array($body["currency"],$allowedCurrency))
|
|
return sendError($response,"'currency' is non allowed. Use one of them : ".implode(",",$allowedCurrency));
|
|
|
|
$curl = curl_init();
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_RETURNTRANSFER => 1,
|
|
CURLOPT_URL => 'https://api.cryptonator.com/api/ticker/'.$body["currency"].'-usd'
|
|
));
|
|
$result = curl_exec($curl);
|
|
curl_close($curl);
|
|
|
|
$result = json_decode($result,true);
|
|
if($result){
|
|
$logger->debug("Response API",$result);
|
|
$response->getBody()->write(json_encode(array("text"=>sprintf("%s-USB = %02f",$body["currency"],$result["ticker"]["price"]))));
|
|
}else
|
|
return sendError($response,"Cryptonator API not available");
|
|
|
|
}
|