Kolby
Kolby

Reputation: 2865

CodeIgniter - Why does library load fine in view but not in controller

I'm using Stripe's php library. DL link: https://code.stripe.com/stripe-php-latest.zip

I've included the library like so inside of a view: require_once(APPPATH.'libraries/stripe/lib/stripe.php');

Everything works fine when I do this in a view, but if I attempt to do it in a controller I get a server error. Why?

I have tried codeigniters load library method, but still a server error. I've change all capitals to lowercases, still the error.

Upvotes: 1

Views: 1589

Answers (4)

rubai
rubai

Reputation: 81

You can use the stripe native library by adding as codeigniter helper.

step 1: put the stripe package into helper directory

step 2: rename the init.php to init_helper.php

step 3: load the helper into your controller

$this->load->helper('stripe/init');

step 4: call stripe library

try 
{
    // set api key
    \Stripe\Stripe::setApiKey('YOUR_SECRET_STRIPE_API_KEY');

    // create customer
    $customer_detail = \Stripe\customer::create(array(
                        'email'    => '[email protected]'
                    ));

    echo $customer_detail->id;
}
catch (Exception $e) 
{
    $error = $e->getMessage();
    echo $error;
}

Upvotes: 0

Robin
Robin

Reputation: 443

If you use third_party libraries, like for example the Facebook PHP SDK, use the following code:

$this->_obj->load->add_package_path(APPPATH.'third_party/facebook/');
$this->_obj->load->library('facebook', $config);    
$this->_obj->load->remove_package_path(APPPATH.'third_party/facebook/');

And place the files for this library in the /third_party/facebook/libraries/ folder.

Note: It's a common mistake to place the files directly in '/facebook/' instead of in '/libraries/' (and also not mentioned in the documentation, too bad).

Upvotes: 1

Bora
Bora

Reputation: 10717

Loading Packages:

$this->load->add_package_path(APPPATH.'third_party/stripe/');
$this->load->library('stripe');

Documentation: Application "Packages" section

http://ellislab.com/codeigniter/user-guide/libraries/loader.html

Upvotes: 1

Carlos Goce
Carlos Goce

Reputation: 1665

Try putting your library on application/libraries and then load your librarie with $this->load->libraries('Stripe.php');

Upvotes: -1

Related Questions