douzhan8652
douzhan8652
2018-04-14 19:58
浏览 186
已采纳

无法使用composer vendor作为codeigniter库

I am trying to use this (https://github.com/Achse/geth-jsonrpc-php-client) library using composer and codeigniter. But I am getting below error:

Type: Error

Message: Class 'GuzzleClient' not found

Filename: /var/www/html/test/application/libraries/Ethereum.php

Line Number: 7

And below is my library code:

<?php

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

        return $client;
    }

}

Composer is wel loaded as other dependancies are working fine. Not able to find out the issue. plz help

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用它( https://github.com/Achse/geth-jsonrpc-php-client )使用composer和codeigniter的库。 但我得到以下错误:

 类型:错误
 
消息:未找到类'GuzzleClient'
 
文件名:/ var / www / html / test / application  /libraries/Ethereum.php
nnLine Number:7 
   
 
 

以下是我的图书馆代码:

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

随着其他依赖项工作正常,Composer已加载。 无法找出问题所在。 请帮助

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwen7371
    dongwen7371 2018-04-14 20:19
    已采纳

    When using Composer you typically have to include an autoload.php file. You also need to used namespaces for the Composer dependencies.

    Usually the vendor directory is on the same level as application so it's handy to define a constant to point to the vendor directory.

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

    Your library maybe needs to look more like this.

    <?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']);
        }
    }
    
    点赞 评论

相关推荐