I have a Symfony 6.4 controller that accepts a JSON, retrieves the values from the JSON, sets the arguments and calls a Symfony Command with these arguments. The Command executes certain API calls and generates an array. The Command works as desired when called from the CLI using php bin/console. However when I call the Controller using CURL I get a blank response. My issue is similar to the one described here but the answers provided don't work.
My Controller :
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\RequestMatcher\IsJsonRequestMatcher;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
class CoupleRateFetchController extends AbstractController
{
#[Route('/couple/rate/fetch', name: 'app_couple_rate_fetch', methods: ['POST'])]
public function index(Request $request, KernelInterface $kernel): Response
{
# 1. Check if request is a JSON otherwise return date_get_last_errors
$jsonMatcher = new IsJsonRequestMatcher();
if ($jsonMatcher->matches($request)) {
# 2. Fetch the request parameters from the Request
$requestparams = json_decode($request->getContent(), true);
# 3. Check if all the parameters are present
if (isset($requestparams["param1"]) && isset($requestparams["Param2"])) {
# 4. Call the command with the parameters
$application = new Application($kernel);
$application->setAutoExit(false);
# 4.4 Call Command
$input = new ArrayInput([
'command' => 'couple:rate-fetch',
'arg1' => $requestparams["param1"],
'arg2' => $requestparams["param2"],
]);
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
}
}
}
# 6. Return the response
return new Response($content);
}
My Command : (Works when run from the CLI)
class CoupleRateFetchCommand extends Command
{
private $entityManager;
public function __construct(private LoggerInterface $logger, EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::REQUIRED, 'Description 1')
->addArgument('arg2', InputArgument::REQUIRED, 'Description 2')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$tof = $input->getArgument('arg1');
$sn = $input->getArgument('arg2');
# 1. Generate the JSON from the request parameters
.
.
.
.
# 2. Generate the payload
.
.
# 3. Fetch and check if the bearer token is json_valid
.
# 4. Generate the headers
.
.
.
# 5. Validate the response if its an error or an estimate
.
.
.
if ($server_response['responseCode'] == 'SUCCESS') {
# Set the array
$output = array(
.
.
);
} else { // if (!$iserrorresponse)
# Return array with the error message
$output = array(
.
.
);
}
} else { // if (json_validate($server_response))
# Return array with Not a valid JSON response
if (!$server_response) {
# Blank response returned if token is invalid
$output = array(
.
.
);
}
}
curl_close($ch1);
# 6. Return the array to the controller
return Command::SUCCESS;
}
}
The Symfony logs show two entries.
[2025年05月11日T12:11:52.515113+05:30] deprecation.INFO: User Deprecated: Since api-platform/core 3.3: Use a "ApiPlatform\State\ProviderInterface" as first argument in "ApiPlatform\Symfony\EventListener\QueryParameterValidateListener" instead of "ApiPlatform\ParameterValidator\ParameterValidator". {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since api-platform/core 3.3: Use a \"ApiPlatform\\State\\ProviderInterface\" as first argument in \"ApiPlatform\\Symfony\\EventListener\\QueryParameterValidateListener\" instead of \"ApiPlatform\\ParameterValidator\\ParameterValidator\". at /home/sridhar/Projects/tms/vendor/api-platform/core/src/Symfony/EventListener/QueryParameterValidateListener.php:47)"} []
[2025年05月11日T12:11:52.531614+05:30] request.INFO: Matched route "app_couple_rate_fetch". {"route":"app_couple_rate_fetch","route_parameters":{"_route":"app_couple_rate_fetch","_controller":"App\\Controller\\CoupleRateFetchController::index"},"request_uri":"http://127.0.0.1:8000/couple/rate/fetch","method":"POST"} []
Not sure if these are relevant to the problem I am facing. I would like to get this working. Any idea what I am doing worng?