Google Sheets API:调用未定义函数GuzzleHttp \ Psr7 \ stream_for()

时间:2019-12-06 15:37:35

标签: php wordpress google-sheets google-sheets-api

我目前正在开发一个WordPress插件,该插件可以从用户那里获取数据并将其附加到Google Spreadsheet,该功能在该插件上进行了一些修改之前可以正常工作,但是不再起作用,我可以似乎无法使其正常工作,无论我做什么,我都会不断收到与Sheets库相关的错误消息。

[06-Dec-2019 15:19:04 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function GuzzleHttp\Psr7\stream_for() in C:\laragon\www\wp\wp-content\plugins\so-application\includes\sheet\vendor\guzzlehttp\psr7\src\Request.php:54
Stack trace:
#0 C:\laragon\www\wp\wp-content\plugins\so-application\includes\sheet\src\Google\Service\Resource.php(201): GuzzleHttp\Psr7\Request->__construct('POST', Object(GuzzleHttp\Psr7\Uri), Array, '{"values":{"val...')
#1 C:\laragon\www\wp\wp-content\plugins\so-application\includes\sheet\vendor\google\apiclient-services\src\Google\Service\Sheets\Resource\SpreadsheetsValues.php(65): Google_Service_Resource->call('append', Array, 'Google_Service_...')
#2 C:\laragon\www\wp\wp-content\plugins\so-application\index.php(52): Google_Service_Sheets_Resource_SpreadsheetsValues->append('7aczdGugRueztuJ...', 'A1', Object(Google_Service_Sheets_ValueRange), Array, Array)
#3 C:\laragon\www\wp\wp-includes\class-wp-hook.php(288): SO_Plugin->initialize('')
#4 C:\laragon\www\wp\wp-includes\class-wp-hook.php(312): WP_Hook->apply_filters('', in C:\laragon\www\wp\wp-content\plugins\so-application\includes\sheet\vendor\guzzlehttp\psr7\src\Request.php on line 54

我尝试更新Sheets客户端库,但没有任何效果,为了缩小错误的范围,我制作了一个单独的插件,重点关注该问题,以确保没有其他冲突,下面是该类的类。插件:

<?php
/*
    Plugin Name: SO Plugin
    */

// If this file is called directly, abort.

if (!defined('ABSPATH')) {
    exit; //Exit if accessed directly
}

define('WP_SO_PLUGIN_VERSION', '1.0.0');
define('WP_SO_PLUGIN_PATH', plugin_dir_path(__FILE__));

class SO_Plugin
{
    function __construct()
    {
        $this->load_libs();
        $this->init_hooks();
    }

    private function init_hooks()
    {
        add_action('wp_ajax_nopriv_initialize', array($this, 'initialize'));
        add_action('wp_ajax_initialize', array($this, 'initialize'));
    }

    private function initGoogleSheetsApi()
    {
        putenv('GOOGLE_APPLICATION_CREDENTIALS=' . WP_SO_PLUGIN_PATH . 'includes/sheet/serviceAccount.json'); // The service account 
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
        $client->setApplicationName('SO Application');
        $service = new Google_Service_Sheets($client);

        return $service;
    }

    public function initialize()
    {
        $service = $this->initGoogleSheetsApi();
        $spreadsheetId = "xxxxx"; // <- placeholder for spreadsheet ID 
        $range = "A1";
        $valueRange = new Google_Service_Sheets_ValueRange();

        $valueRange->setValues(["values" => ["a", "b"]]);
        $conf = ["valueInputOption" => "RAW"];
        $ins = ["insertDataOption" => "INSERT_ROWS"];

        $result = $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf, $ins);

        if ($result) {
            $range = $result['updates']['updatedRange'];
            echo $range;
        }
        exit;
    }

    public function load_libs()
    {
        require_once WP_SO_PLUGIN_PATH . 'includes/sheet/vendor/autoload.php';
    }
}


new SO_Plugin();

要运行该功能,请访问此WordPress挂钩:

http://localhost/wp/wp-admin/admin-ajax.php?action=initialize

0 个答案:

没有答案