1

I am trying to ingrate mailchimp API API link with woocommerce. I am trying to add user in mailchimp. Before adding the user in mailchimp list , i searching the user with getListMember method if user is not present in mailchimp list , i will add the user to list But if user exist in the list i will update the status. For searching the user i created a custom function getMemberInfo($email) i am calling this function before add user in mailchimp list

function getMemberInfo($email)
{
    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
    'apiKey' => $GLOBALS['mailchimpAPI'],
    'server' => $GLOBALS['mailchimpServer']
    ]);

    $list_id = $GLOBALS['mailchimpListID'];
    $subscriberHash = md5(strtolower($email));


    try {
        $response = $mailchimp->lists->getListMember($list_id, $subscriberHash);
        //print_r($response);
   
        if($response->status == 404)
            {
                return 0;
            }else{
                return 1;
            }
    } catch (MailchimpMarketing\ApiException $e) {
        echo $e->getMessage();
    }

}

but i am getting the following error

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://usxx.api.mailchimp.com/3.0/lists/debxxxxxxxx/members/xxxxxxxxxd207ccxxxxxxx6a5d238` resulted in a `404 Not Found` response:
{"type":"https://mailchimp.com/developer/marketing/docs/errors/","title":"Resource Not Found","status":404,"detail":"The (truncated...)
 in D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113
Stack trace:
#0 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\guzzle\src\Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL)
#1 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL)
#3 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#4 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(248): GuzzleHttp\Promise\TaskQueue->run(true)
#5 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn()
#6 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending()
#7 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList()
#8 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\promises\src\Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending()
#9 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\guzzle\src\Client.php(123): GuzzleHttp\Promise\Promise->wait()
#10 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\mailchimp\marketing\lib\Api\ListsApi.php(4218): GuzzleHttp\Client->send(Object(GuzzleHttp\Psr7\Request), Array)
#11 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\mailchimp\marketing\lib\Api\ListsApi.php(4207): MailchimpMarketing\Api\ListsApi->getListMemberWithHttpInfo('debxxxxxxxx', 'xxxxxxxxxd207ccxxxxxxx6a5d238...', NULL, NULL)
#12 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\mailchimp-api.php(80): MailchimpMarketing\Api\ListsApi->getListMember('debxxxxxxxx', 'xxxxxxxxxd207ccxxxxxxx6a5d238...')
#13 D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\functions.php(351): getMemberInfo('test1009@test...')
#14 D:\xampp\htdocs\wpreg\wp-includes\class-wp-hook.php(303): save_my_account_page_custom_details(33)
#15 D:\xampp\htdocs\wpreg\wp-includes\class-wp-hook.php(327): WP_Hook->apply_filters('', Array)
#16 D:\xampp\htdocs\wpreg\wp-includes\plugin.php(470): WP_Hook->do_action(Array)
#17 D:\xampp\htdocs\wpreg\wp-content\plugins\woocommerce\includes\class-wc-form-handler.php(348): do_action('woocommerce_sav...', 33)
#18 D:\xampp\htdocs\wpreg\wp-includes\class-wp-hook.php(303): WC_Form_Handler::save_account_details('')
#19 D:\xampp\htdocs\wpreg\wp-includes\class-wp-hook.php(327): WP_Hook->apply_filters(NULL, Array)
#20 D:\xampp\htdocs\wpreg\wp-includes\plugin.php(470): WP_Hook->do_action(Array)
#21 D:\xampp\htdocs\wpreg\wp-includes\template-loader.php(13): do_action('template_redire...')
#22 D:\xampp\htdocs\wpreg\wp-blog-header.php(19): require_once('D:\\xampp\\htdocs...')
#23 D:\xampp\htdocs\wpreg\index.php(17): require('D:\\xampp\\htdocs...')
#24 {main}
  thrown in D:\xampp\htdocs\wpreg\wp-content\themes\ljschildtheme\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

Update

For adding the user i am using the flowing function.This function is working fine

function subscribeEmailToMailChimp($email,$fname,$lname)
{
    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
    'apiKey' => $GLOBALS['mailchimpAPI'],
    'server' => $GLOBALS['mailchimpServer']
    ]);

    $list_id = $GLOBALS['mailchimpListID'];

    try {
        $response = $mailchimp->lists->addListMember($list_id, [
        "email_address" => $email,
        "status" => "subscribed",
        "merge_fields" => [
        "FNAME" => $fname,
        "LNAME" => $lname
        ]
        ]);
    //print_r($response);
    } catch (MailchimpMarketing\ApiException $e) {
        echo $e->getMessage();
    }

}

Any Advice

Siddharth
  • 99
  • 10

3 Answers3

1

i resolve this like this. Hope it helps!

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
//use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Exception\ClientException;

$mailchimp = new MailchimpMarketing\ApiClient();
$mailchimp->setConfig([
    'apiKey' => $apiKey,
    'server' => $serverPrefix
]);

try {
    $response = $mailchimp->lists->getListMember($audienceID, $userHash);
    print_r($response);
} catch (ClientException $e) {
    echo $e->getMessage();
    echo Psr7\Message::toString($e->getRequest());
    echo Psr7\Message::toString($e->getResponse());
}
Sebastian
  • 11
  • 3
  • 1
    Hi, and thank you for taking the time to write this answer! While this code may answer the question, it may be more useful for other people if you include some context, explaining how it works and when to use it.. [Code-only answers are generally discouraged on StackOverflow.)[https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers] – elaforma Oct 11 '22 at 22:07
0

In my case I solved by using \EXCEPTION instead of MailchimpMarketing\ApiException

Lorenzo Magon
  • 544
  • 5
  • 17
0

If you look at the MailChimp API client library, you'll see that it returns a RequestException on an error, and Guzzle (the underlying HTTP client) throws an exception for a 404 (not found) response.

With this in mind you can see why your code isn't working.

It's trying a $mailchimp->lists->getListMember() call, but finding that the user doesn't exist on the mailing list and so getting a 404. That 404 is throwing a RequestException, but your code is only catching an ApiException.

If you add RequestException to your catch, you can then deal with these 404 errors.

For example:

try {
    $response = $mailchimp->lists->getListMember($list_id, $subscriberHash);

} catch (RequestException $e) {
    if($e->getCode() === 404) {
        echo "User wasn't found on mailing list"; // Don't literally do this, just showing you where the error is
    }
} catch (MailchimpMarketing\ApiException $e) {
    echo $e->getMessage();
}
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289