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

peterlc's avatar

Run artisan/composer command in plain php

Hi guys,

Can you please guide me how i can run artisan/composer command in plain php?

Found this:

echo system('php artisan down');
echo exec('php artisan down');
exec('/usr/bin/php '.base_path().'/artisan down');

but nothing works.

i would like to run some things while deploying, is this possible in this code?

<?php
const API_KEY = "xxxxxxxxxxxxxxx";
const API_URL = "xxxxxxxxxxxxxxx";
const EMAIL = "xxxxxxxxxxxxxxx";

const BranchName = "xxxxxxxxxxxxxxx";
const GitUrl = "xxxxxxxxxxxxxxx";

//Use this function to contact CW API
function callCloudwaysAPI($method, $url, $accessToken, $post = [])
{   
    $baseURL = API_URL;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Set Authorization Header
    if ($accessToken) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken]);
    }

    //Set Post Parameters
    $encoded = '';
    if (count($post)) {
        foreach ($post as $name => $value) {
            $encoded .= urlencode($name) . '=' . urlencode($value) . '&';
        }
        $encoded = substr($encoded, 0, strlen($encoded) - 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpcode != '200') {
        die('An error occurred code: ' . $httpcode . ' output: ' . substr($output, 0, 10000));
    }
    curl_close($ch);
    return json_decode($output);
}
//Fetch Access Token
$tokenResponse = callCloudwaysAPI('POST', '/oauth/access_token', null
    , [
        'email' => EMAIL,
        'api_key' => API_KEY
    ]);
$accessToken = $tokenResponse->access_token;
$gitPullResponse = callCloudWaysAPI('POST', '/git/pull', $accessToken, [
    'server_id' => $_GET['server_id'],
    'app_id' => $_GET['app_id'],
    'git_url' => $_GET['git_url'],
    'branch_name' => $_GET['branch_name']
    /* Uncomment it if you want to use deploy path, Also add the new parameter in your link
    'deploy_path' => $_GET['deploy_path']
    */
]);
echo (json_encode($gitPullResponse));

Thank you.

0 likes
4 replies
martinbean's avatar

@peterlc Why are you trying to call Artisan from a PHP script? Just call the Artisan commands you need to in your deployment steps.

peterlc's avatar

Well @martinbean im fiddeling with another solution than Envoyer :)

Or what do you mean by "deployment steps"?

Please or to participate in this conversation.