代码之家  ›  专栏  ›  技术社区  ›  Haren Sarma

无法将composer供应商用作codeigniter库

  •  1
  • Haren Sarma  · 技术社区  · 6 年前

    我想用这个( https://github.com/Achse/geth-jsonrpc-php-client )使用composer和codeigniter的库。但我有以下错误:

    Type: Error
    
    Message: Class 'GuzzleClient' not found
    
    Filename: /var/www/html/test/application/libraries/Ethereum.php
    
    Line Number: 7
    

    下面是我的库代码:

    <?php
    
    class Ethereum
    {
        public function __construct()
        {
            $httpClient = new GuzzleClient(new GuzzleClientFactory(), 'localhost', 8545);
            $client     = new Client($httpClient);
    
            return $client;
        }
    
    }
    

    作曲家很受欢迎,因为其他依赖项工作正常。无法发现问题。plz帮助

    1 回复  |  直到 6 年前
        1
  •  0
  •   DFriend    6 年前

    使用Composer时,通常必须包括 autoload.php 文件您还需要为Composer依赖项使用名称空间。

    通常是 vendor 目录与位于同一级别 application 因此,定义一个常量来指向供应商目录非常方便。

    define('VENDOR', substr(FCPATH, 0, strpos(APPPATH, 'application/')) . "vendor/");
    

    你的图书馆可能需要看起来更像这样。

    <?php
    require VENDOR . 'autoload.php';
    
    //I'm not certain this is the correct namespace but hopefully you get the idea
    use Achse\GethJsonRpcPhpClient; 
    
    class Ethereum
    {
        protected $httpClient;
        protected $client;
    
        public function __construct()
        {
            $this->httpClient = new GuzzleClient(new GuzzleClientFactory(), 'localhost', 8545);
            $this->client     = new Client($httpClient);
        }
    
        //example using the class property $client
        public getResult()
        {
            return $this->client->callMethod('eth_getBalance', ['0xf99ce9c17d0b4f5dfcf663b16c95b96fd47fc8ba', 'latest']);
        }
    }