when I run my file through command line, I am receiving below message,
Method not allowed. Must be one of: GET
when I execute same code from the browser it runs perfectly fine.
Blow is the code of my public file
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$file = __DIR__ . $_SERVER['REQUEST_URI'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes/api_parse_csv.php';
// Run app
$app->run();
If you call a PHP file directly without a web browser (which is what I suspect you are doing with cron) there is no HTTP method set. This is what the method not allowed error is suggesting. Probably none of your routes match the request.
Check the docs on routing and you could try the any or map instead of get, but I can’t say for sure if that will work.
Alternatively, you could change your cron job so that it does something like a wget request to the same URL as you hit in your browser, instead of just running it directly from PHP. In that case an appropriate HTTP method would be set.
Did you try calling it using curl? That way you’re issuing a HTTP request and things should work as intended (and it can easily be scripted using a shell script).
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$file = DIR . $SERVER['REQUESTURI'];
if (is_file($file)) {
return false;
}
}
require DIR . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require DIR . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require DIR . '/../src/dependencies.php';
// Register middleware
require DIR . '/../src/middleware.php';
// Register routes
require DIR . '/../src/routes/api_parse_csv.php';
// Run app
$app->run();