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

时间:2016-02-05 16:11:51

标签: php shell oauth-2.0 google-drive-api 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执行。当我尝试从控制台执行此操作时,没有任何反应,因为会话变量和重定向是它无法处理的事情。如何修改此脚本以便从控制台执行?

1 个答案:

答案 0 :(得分:2)

只需定义/设置变量:$_GET, $_SESSION, ....
这些变量可以像使用$argv[]

的控制台中的参数一样传递