I created a JSON with composer to get the price of the BRL, EUR, USD coin. The program is working perfectly, however when I use the command in the cmd "php -S localhost:8000 index.php" the program is work, but function "isset($_GET['url'])" is not working in this case. FOr example, when I use "localhost:8000/exchange", need show the error: "echo json_encode(array('status' => 'error 3'));", but appear the error: "echo json_encode(array('status' => 'error 1'));"
The code of the index.php is here:
<?php
/**
* Back-end Challenge.
*
* PHP version 7.2
*
* Este será o arquivo chamado na execução dos testes automátizados.
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
declare(strict_types=1);
header('Content-Type: application/json');
require __DIR__ . '/vendor/autoload.php';
//require_once './vendor/autoload.php';
if (isset($_GET['url'])) {
$url = explode('/', $_GET['url']);
//echo $url[0];
if ($url[0] === "exchange") {
ucfirst($url[0]);
$service = 'App\Services\\'.ucfirst($url[0]).'Service';
array_shift($url);
if(is_numeric ($url[0])){
$method = strtolower($_SERVER['REQUEST_METHOD']);
try {
$response = call_user_func(array(new $service, $method), $url);
if($response == "error"){
http_response_code(400);
echo json_encode(array('status' => 'error 5'));
}else{
http_response_code(200);
echo json_encode(array('valorConvertido' => $response->getValor(), 'simboloMoeda' => $response->getSimbolo()));
}
exit;
} catch (\Exception $e) {
http_response_code(400);
echo json_encode(array('status' => 'error 4', 'data' => $e->getMessage()), JSON_UNESCAPED_UNICODE);
exit;
}
}else{
http_response_code(400);
echo json_encode(array('status' => 'error 3'));
exit;
}
}else{
http_response_code(400);
echo json_encode(array('status' => 'error 2'));
exit;
}
}else{
http_response_code(400);
echo json_encode(array('status' => 'error 1')); //Always appear this error
exit;
}
Someone can help me?
CodePudding user response:
You can try the GLOBAL variable $_SERVER['REQUEST_URI'] or maybe parse url to get the url.
CodePudding user response:
To make get requests working in cmd use this:
//check if is commandline or web
$is_website=http_response_code()!==FALSE;
if(!$is_website){
parse_str(implode('&', array_slice($argv, 1)), $_GET);
}
terminal command get request usage example : cd "/Users/fastcomputer/Google Drive/www/mybot" php myphpfile.php username=userid password=pass