0

I have added coinbase php library inside application/third_party folder. Now I want to add a normal php file using that library in codeigniter root folder to add content to log file and send email to notify about transactions?

  • Is it okay if add a core php file in root? How would I require that coinbase plugin and load database into the said php file?
  • Is it a good practice in mvc? What are other alternatives?

UPDATE: I created a controller in application/controlers with below code

class Coinbase extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();   
    }

    function readnotifications()
    {
        echo APPPATH;
        require_once(APPPATH.'third_party/coinbase-php/vendor/autoload.php');
        use Coinbase\Wallet\Client;
}
}

I get below error.

Parse error: syntax error, unexpected 'use' (T_USE)

UPDATE2:

$config['composer_autoload'] = APPPATH.'third_party/coinbase-php/vendor/autoload.php';

class Coinbase extends CI_Controller
{
    function readnotifications()
    {

$apiKey = "sss ";
$apiSecret =  "sss";
        namespace Coinbase\Wallet;
        use Coinbase\Wallet\Client;
        use Coinbase\Wallet\Configuration;
        use Coinbase\Wallet\Enum\Param;
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean
}
}

Parse error: syntax error, unexpected 'Coinbase' (T_STRING), expecting \ (T_NS_SEPARATOR)

Any idea how to resolve?

SMJ
  • 716
  • 1
  • 9
  • 23
  • what version of codeigniter are you using – Siddharth Chaudhery May 19 '18 at 08:40
  • I don't get it. Are you trying to create a controller and are having issues loading a third party lib that might not be ci compatible (namespaced)? Because if you just want a php page you don't need ci and you can access it's db through some hacky ways but I don't see why you don't just create a controller. Having said that no, it's not mvc so you can't consider it good or bad practice because it's something else entirely. – Alex May 19 '18 at 08:51
  • @RahulSingh CI 3.1 – SMJ May 19 '18 at 09:15

1 Answers1

1

In you application/config/config.php search for $config['composer_autoload'] and set you third party package autoload.php file path as its value

$config['composer_autoload'] = '/path/to/vendor/autoload.php';

Then you can simple use any of your package classes like this

function readnotifications()
{
        $client = Coinbase\Wallet\Client;
}

Also have a look at this https://www.codeigniter.com/user_guide/general/autoloader.html

  • thanks for responding. I updated my question. Can you please check it? – SMJ May 19 '18 at 10:04
  • 1
    Don't use namespace and use statements. They should only be used at the starting of any php files according to PSR. Instead use fully qualified class names. Like $configuration = \Coinbase\Wallet\Configuration::apiKey($apiKey, $apiSecret); Give it a try – Siddharth Chaudhery May 19 '18 at 10:14