Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

php - Integrating Mailjet API v3 wrapper as Codeigniter library

How can I integrate the Mailjet API PHP wrapper into my Codeigniter installation as a library?

Is it as simple as placing the contents of the repository into application/libraries/Mailjet and then creating a Mailjet.php file in application/libraries which initializes Mailjet like shown below?

require 'Mailjet/vendor/autoload.php';

use MailjetResources;

$mj = new MailjetClient(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));

Please let me know if I'm on the right track. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you are on right track. But you don't need to create CI library. Use Mailjet repository library in controller as well. Just use composer as stated in CI docs.

If you want CodeIgniter to use a Composer auto-loader, just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.

Step by step instruction for using github repository in CodeIgniter

  1. Set $config['composer_autoload'] = TRUE; in APPPATH.'config/config.php' file
  2. Put composer.json file with wanted repositories/projects in APPPATH location
  3. Do the job with composer install command through console which will make vendor and other related files and folders inside
  4. Use it when needed in controller or in other code as shown in example bellow

example controller Mailman.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

use MailjetResources;

class Mailman extends CI_Controller
{
    private $apikey = 'apy__key__here';
    private $secretkey = 'apy__secret__here';

    protected $mj = NULL;

    public function __construct()
    {
        // $this->mj variable is becoming available to controller's methods
        $this->mj = new MailjetClient($this->apikey, $this->apisecret);
    }

    public function index()
    {
        $response = $this->mj->get(Resources::$Contact);

        /*
         * Read the response
         */
        if ($response->success())
            var_dump($response->getData());
        else
            var_dump($response->getStatus());
    }
}

If you explicitly want to use Mailjet (or any other) repository through CI library, check in docs how to create custom library and merge this code above with it. Personaly I use repositories this way to avoid unnecessarily loading and parsing sufficient libraries.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...