首页 > 解决方案 > 如何在窗口机器上运行 apache 服务器时运行 php 命令输入 STDIN 以接受输入

问题描述

我是 php 新手。我正在运行与谷歌日历 api 交互的代码,它应该接受命令输入以在屏幕上进行验证。我明白了,但我不知道我是否应该有已经打开了命令窗口,因为当程序到达执行代码的文件时,命令行输入没有启动。我有点困惑,几天来一直无法弄清楚这一点。

我是否应该已经启动并运行命令,如果我应该如何使用我当前的 php 文件启动它。当我试图启动它时,我遇到了错误。我假设我在我的 php 顶部使用的命令会自动打开命令让我输入我从网页屏幕获得的验证。任何帮助将不胜感激这是我的设置

apache 网络服务器 2.4
php 7.2.10
窗口

这是我尝试运行此 php 文件时遇到的错误

PHP 警告:require_once(./vendor/autoload.php):无法打开流:第 5 行的 C:\Apache24\htdocs\booking\TrashItToken.php 中没有这样的文件或目录

警告:require_once(./vendor/autoload.php):打开流失败:C:\Apache24\htdocs\booking\TrashItToken.php 第 5 行 PHP 中没有这样的文件或目录致命错误:require_once():需要打开失败'./vendor/autoload.php' (include_path='.;C:\php\pear') 在 C:\Apache24\htdocs\booking\TrashItToken.php 第 5 行

致命错误:require_once():在第 5 行的 C:\Apache24\htdocs\booking\TrashItToken.php 中打开所需的 './vendor/autoload.php' (include_path='.;C:\php\pear') 失败

    <?php
 $stdin = fopen('php://stdin', 'r');
//define('STDIN',fopen("php://stdin","r"));
// include your composer dependencies
require_once './vendor/autoload.php';
#defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */

class AppToken
{
    function getClient()
    {
        $client = new Google_Client();
        $client->setAuthConfig('./xxxxxx.json');
        $client->setApplicationName('xxxxxxxxxxxxx');
        $client->addScope("https://www.googleapis.com/auth/calendar");
        $client->addScope("https://www.googleapis.com/auth/calendar.events");
        $client->setAccessType('offline');
        $client->setPrompt('select_account consent');

        $auth_url = $client->createAuthUrl();
        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

        // Load previously authorized token from a file, if it exists.
        // The file token.json stores the user's access and refresh tokens, and is
        // created automatically when the authorization flow completes for the first
        // time.
         $tokenPath = './includes/token.json';
         if (file_exists($tokenPath)) {
            $msg = "We entered the if statement and found the token.";
            error_log($msg, 0);
             $accessToken = json_decode(file_get_contents($tokenPath), true);
             $client->setAccessToken($accessToken);


         }

     // If there is no previous token or it's expired.
        if ($client->isAccessTokenExpired()) {
            // Refresh the token if possible, else fetch a new one.
            $msg1 = "We didn't find the token or its expired.";
            error_log($msg1, 0);
            if ($client->getRefreshToken()) {
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            } else {
                 // Request authorization from the user.
                $authUrl = $client->createAuthUrl();
                printf("Open the following link in your browser:\n%s\n", $authUrl);
                print 'Enter verification code: ';

                $msg2 = "We request authorization from the user.";
                error_log($msg2, 0);
                $authCode = trim(fgets(STDIN));//this is part where code fails

                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                $client->setAccessToken($accessToken);

                // Check to see if there was an error.
                if (array_key_exists('error', $accessToken)) {
                    $msg3 = "An error occured after access token.";
                    error_log($msg3, 0);
                   throw new Exception(join(', ', $accessToken));
                }
            }
            // Save the token to a file.
            if (!file_exists(dirname($tokenPath))) {
                mkdir(dirname($tokenPath), 0700, true);
            }
            file_put_contents($tokenPath, json_encode($client->getAccessToken()));


        }

        return $client;
    }
}
?>

标签: php

解决方案


我搞砸了我是怎么理解的。我必须更改我的代码才能调用该函数,因为我是从另一个 php 文件调用该函数。因此,当我从编辑器调用 php 文件时,我更改了文件以调用该函数。

$myapptoken = new AppToken();
$mytoken =  $myapptoken->getClient();

我用来运行我的 php 文件的命令

php C:\Apache24\htdocs\xxxxxx\xxxxxxx.php


推荐阅读