2
votes

i am using this mailchimp-api according to the documentation i should just place

use \DrewM\MailChimp\MailChimp;

$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');

directory structure

  • index.php (this is where i call any library i use using use)

  • composer.json

  • vendor (this is the folder that contains all libraries from git and the autoload file)

    -- drewm

    --- mailchimp-api

    ---- src (this folder contains the Mailchimp.php that has namespace DrewM\MailChimp;)

  • configs (config directory)

    -- functions.php (this file contains the function for mailchimp)

now whenever i call this mailchimp() function i get

Fatal error: Uncaught Error: Class 'DrewM\MailChimp\MailChimp' not found

composer.json file

{
    "require": {
        "drewm/mailchimp-api": "^2.4",
        "ircmaxell/random-lib": "^1.2",
        "phpmailer/phpmailer": "^5.2"
    }
}

index.php

require_once './vendor/autoload.php';
require_once './config/functions.php';

functions.php

use \DrewM\MailChimp\MailChimp;
function mailchimp(){
    $MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');
    $result = $MailChimp->get('lists');
    print_r($result);
}
2
How did you install mailchimp-api library? I see you mentioned git, did you just get a copy of code and put it in vendor directory? Can you post content of composer.json? - Nima
@Nima i installed it using composer, its included in composer.json - mirvatJ
Did you add correct namespace mapping for DrewM\MailChimp to composer.json and run composer dump-autoload? - Nima
@Nima yes {"require": { "drewm/mailchimp-api": "^2.4", - mirvatJ
Can you show us the part of code in which you include vendor/autoload.php, your use statements and where you include functions.php? - Nima

2 Answers

1
votes

Probably the function class is not under the autoloader of Composer. You should add in the autoload section of the composer.json files as example:

"autoload": {
    "psr-0": { "": "./" },
    "files": ["configs/functions.php"]
}

And remove the require_once directive of functions.php from index.php

Hope this help

0
votes

You need to include mailchimp in the functions.php

use \DrewM\MailChimp\MailChimp;

or you can use

function mailchimp(){
    $MailChimp = new \DrewM\MailChimp\MailChimp($apikey);
    $result = $MailChimp->get('lists');
    return $result;
}

and pass the $apikey in the class