Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eddieace's avatar

PUT POST API Calls from as a cronjob

Hi! I have put all my API calls methods in a models. They are basic commands like create() read() search() store() etc.

For a start I made these in a controller and now I'm trying to move the logic from the controller into a commands so I can run the script as a daily cronjob. For some reason they wont work as commands. Is this because you cant use PUT POST calls? Since it seams that the read functions works just fine.

And if it is so that i cant use PUT and POST in crons, is there a workaround for this?

Here is some example code

public function handle()
{
    $myModel = new MyModel;
    $thing = array(
        "title" => "My title",
        "user_id" => 12345,
        "org_id" => 54321,
    );

    $returned_id = $myModel->create($thing);
}


public function create($thing)
{
    $api_token = "XXXX";
    $url = "https://api.api-site.com/thing?api_token=" . $api_token;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $thing);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    //array from the data that is sent back from the API
    $result = json_decode($output, 1);
    // check if an id came back
    if (!empty($result['data']['id'])) {
    $deal_id = $result['data']['id'];
    return $deal_id;
    } else {
    return false;
    }
}
0 likes
1 reply
eddieace's avatar

This is an answer I got from another source.

"GET PUT POST DELETE HEAD are all http calls, these methods are determined by you framework routing by reading the HTTP headers >and the calls are then redirected to appropriate function by the same routing. When you run a cronjob you are using CLI not HTTP so there >are no headers and by default everything is interpreted as GET. REST methods are not ment and neither should be used anywhere else >except http API calls. Now depending on you app loginc there are few things you can do:

Create a propper controller for CLI that manages which methods to access (better way). Create a script that uses CURL to access your via HTTP(s) with required HTTP method. (hacky way) Rework your app so that REST API functionality does not need to be triggered from cron as they should not be tied in any way. (correct way)"

But this does not really help me because my GET requests work.

Please or to participate in this conversation.