Add initial support for PHP7 (#97)
* 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
This commit is contained in:
committed by
Soam Vasani
parent
c353e7f230
commit
2725da6b1b
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
echo "Hello from PHP";
|
||||
$logger->warning("Hello logger");
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
function handler($context){
|
||||
/** @var \Psr\Http\Message\ResponseInterface $response */
|
||||
$response = $context["response"];
|
||||
/** @var \Psr\Http\Message\ServerRequestInterface $request */
|
||||
$request = $context["request"];
|
||||
/** @var \Psr\Log\LoggerInterface $logger */
|
||||
$logger = $context["logger"];
|
||||
|
||||
$response->getBody()->write("Hello from handler PHP");
|
||||
$logger->warning("Hello logger");
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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");
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user