将 PHP 脚本转换为可执行文件


Transform PHP script into executable

我正在尝试创建一个脚本来将我的文件上传到Google云端硬盘。当我运行它时,它工作得很好。这是工作代码:

public function uploadToDrive()
{
    $client = new Google_Client();
    $client->setClientId(Mage::getStoreConfig('mtm_google/drive/client_id'));
    $client->setClientSecret(Mage::getStoreConfig('mtm_google/drive/client_secret'));
    $client->setRedirectUri(Mage::getStoreConfig('mtm_google/drive/redirect_uri'));
    $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
    session_start();
    if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
        } else {
            $client->setAccessToken($_SESSION['access_token']);
        }
        $service = new Google_Service_Drive($client);
        $file = new Google_Service_Drive_DriveFile();
        $file->setName('analysis.csv');
        $file->setDescription('A test document');
        $data = file_get_contents('temp.csv');
        $service->files->create($file, array(
            'data' => $data,
            'mimeType' => 'text/csv',
            'uploadType' => 'multipart'
        ));
        echo 'SUCCESS!';
        unlink('temp.csv');
    } else {
        $authUrl = $client->createAuthUrl();
        header('Location: ' . $authUrl);
        exit();
    }
}

此脚本应可从控制台执行,而不是从某个 URL 执行。当我尝试从控制台执行此操作时,没有任何反应,因为会话变量和重定向是它无法处理的。如何修改此脚本以便从控制台执行?

简单地定义/设置变量:$_GET, $_SESSION, ....
这些变量可以像参数一样从控制台使用 $argv[]