第一次出现Google表格更新问题

时间:2018-11-06 09:14:19

标签: php google-sheets google-sheets-api internal-server-error

我在更新Google Sheet数据时遇到问题。为了更新数据,我使用的是this库,如果您调用直接文件并且在本地环境中运行,它可以正常工作

但是问题是当我从服务器运行以更新它提供的数据时

  

500内部错误

,如果刷新页面,它将正常工作。

基本上不是第一次。

根据我的error_log文件,错误为

  

警告:htmlspecialchars()期望参数1为字符串

    <?php
require 'vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

//error_reporting(E_ALL);
//ini_set('display_errors', 'On');

class APIClass {
    function __construct($token){

        $serviceRequest = new DefaultServiceRequest($token);
        ServiceRequestFactory::setInstance($serviceRequest);
    }
    function updateNow($sku,$quantity){

        $this->updateNow($sku,$quantity);
        $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
        $spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();

        $spreadsheet = $spreadsheetFeed->getByTitle('product');
        $worksheetFeed = $spreadsheet->getWorksheetFeed();
        $worksheet = $worksheetFeed->getByTitle('Sheet2');
        $listFeed = $worksheet->getListFeed();

        $Entries = $listFeed->getEntries();


        $i = 0;
        $update = false;
        foreach ($listFeed->getEntries() as $entry) {
            $values[$i] = $entry->getValues();
            $i++;
        }

        for($k=0;$k<=$i;$k++){
            for($j=1;$j<=16;$j++){
                if($values[$k]['sku'] == $sku){
                    $listEntry = $Entries[$k];
                    $values['quantity'] = $quantity;
                    $listEntry->update($values);
                    $update = true;
                }
            }
        }
    }
}
function getClient()
    {
        $client = new Google_Client();
        $client->setApplicationName('Google Sheets API PHP Quickstart');
        $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
        $client->setAuthConfig('credentials.json');
        $client->setAccessType('offline');
        $client->setPrompt('select_account consent');

        // Load previously authorized token from a file, if it exists.
        $tokenPath = 'token.json';
        if (file_exists($tokenPath)) {
            $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.
            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: ';
                $authCode = trim(fgets(STDIN));

                // Exchange authorization code for an access token.
                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

                //$client->setAccessToken($accessToken);
                $client->setAccessToken($accessToken);

                // Check to see if there was an error.
                if (array_key_exists('error', $accessToken)) {
                    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;
    }
 $client = getClient();
$obj = new APIClass($client->getAccessToken()['access_token']);
$obj->updateNow("Temp2","100");
?>

我们将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

最后发现我正在传递多维数据,并将其传递给foreach后成为单个数组,但是我需要的是字符串值,因此通过传递单个维数组来解决它也将您的代码放入try catch块中,这阻止了我获取' BadRequestException ”(来自Google Sheet API

谢谢你们。