jeudi 10 novembre 2016

PUT and POSTS AS Cron commands PHP Laravel 5

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

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

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;
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire