skip to Main Content

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 MailchimpMarketingApiClient();

    $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 (MailchimpMarketingApiException $e) {
        echo $e->getMessage();
    }

}

but i am getting the following error

PHP Fatal error:  Uncaught GuzzleHttpExceptionClientException: 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:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttpguzzlesrcExceptionRequestException.php:113
Stack trace:
#0 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttpguzzlesrcMiddleware.php(69): GuzzleHttpExceptionRequestException::create(Object(GuzzleHttpPsr7Request), Object(GuzzleHttpPsr7Response), NULL, Array, NULL)
#1 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(204): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Response))
#2 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(153): GuzzleHttpPromisePromise::callHandler(1, Object(GuzzleHttpPsr7Response), NULL)
#3 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcTaskQueue.php(48): GuzzleHttpPromisePromise::GuzzleHttpPromise{closure}()
#4 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(248): GuzzleHttpPromiseTaskQueue->run(true)
#5 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(224): GuzzleHttpPromisePromise->invokeWaitFn()
#6 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(269): GuzzleHttpPromisePromise->waitIfPending()
#7 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(226): GuzzleHttpPromisePromise->invokeWaitList()
#8 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttppromisessrcPromise.php(62): GuzzleHttpPromisePromise->waitIfPending()
#9 D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttpguzzlesrcClient.php(123): GuzzleHttpPromisePromise->wait()
#10 D:xampphtdocswpregwp-contentthemesljschildthemevendormailchimpmarketinglibApiListsApi.php(4218): GuzzleHttpClient->send(Object(GuzzleHttpPsr7Request), Array)
#11 D:xampphtdocswpregwp-contentthemesljschildthemevendormailchimpmarketinglibApiListsApi.php(4207): MailchimpMarketingApiListsApi->getListMemberWithHttpInfo('debxxxxxxxx', 'xxxxxxxxxd207ccxxxxxxx6a5d238...', NULL, NULL)
#12 D:xampphtdocswpregwp-contentthemesljschildthememailchimp-api.php(80): MailchimpMarketingApiListsApi->getListMember('debxxxxxxxx', 'xxxxxxxxxd207ccxxxxxxx6a5d238...')
#13 D:xampphtdocswpregwp-contentthemesljschildthemefunctions.php(351): getMemberInfo('test1009@test...')
#14 D:xampphtdocswpregwp-includesclass-wp-hook.php(303): save_my_account_page_custom_details(33)
#15 D:xampphtdocswpregwp-includesclass-wp-hook.php(327): WP_Hook->apply_filters('', Array)
#16 D:xampphtdocswpregwp-includesplugin.php(470): WP_Hook->do_action(Array)
#17 D:xampphtdocswpregwp-contentpluginswoocommerceincludesclass-wc-form-handler.php(348): do_action('woocommerce_sav...', 33)
#18 D:xampphtdocswpregwp-includesclass-wp-hook.php(303): WC_Form_Handler::save_account_details('')
#19 D:xampphtdocswpregwp-includesclass-wp-hook.php(327): WP_Hook->apply_filters(NULL, Array)
#20 D:xampphtdocswpregwp-includesplugin.php(470): WP_Hook->do_action(Array)
#21 D:xampphtdocswpregwp-includestemplate-loader.php(13): do_action('template_redire...')
#22 D:xampphtdocswpregwp-blog-header.php(19): require_once('D:\xampp\htdocs...')
#23 D:xampphtdocswpregindex.php(17): require('D:\xampp\htdocs...')
#24 {main}
  thrown in D:xampphtdocswpregwp-contentthemesljschildthemevendorguzzlehttpguzzlesrcExceptionRequestException.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 MailchimpMarketingApiClient();

    $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 (MailchimpMarketingApiException $e) {
        echo $e->getMessage();
    }

}

Any Advice

3

Answers


  1. i resolve this like this. Hope it helps!

    require 'vendor/autoload.php';
    use GuzzleHttpClient;
    use GuzzleHttpPsr7;
    use GuzzleHttpPsr7Request;
    //use GuzzleHttpPsr7Message;
    use GuzzleHttpExceptionClientException;
    
    $mailchimp = new MailchimpMarketingApiClient();
    $mailchimp->setConfig([
        'apiKey' => $apiKey,
        'server' => $serverPrefix
    ]);
    
    try {
        $response = $mailchimp->lists->getListMember($audienceID, $userHash);
        print_r($response);
    } catch (ClientException $e) {
        echo $e->getMessage();
        echo Psr7Message::toString($e->getRequest());
        echo Psr7Message::toString($e->getResponse());
    }
    
    Login or Signup to reply.
  2. In my case I solved by using EXCEPTION instead of MailchimpMarketingApiException

    Login or Signup to reply.
  3. 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 (MailchimpMarketingApiException $e) {
        echo $e->getMessage();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search